Źródło:
Komentarze (0)
| | źródło pochodzi od: sympozjon Opis: Zbiór funkcji obsługujących PostgreSQL. Zawiera funkcje obsługi błędów. Język: php |
<?php
/******************************************************************************* **********************
* PostgreSQL Connection / Query Cl
ass *
* *
* Purpose: To encapsulate PostgreSQL Connections and Query's in an e
asy to use cl
ass, with error *
* handling at the object level. *
* *
* Programmed by Andrew Tejero *
******************************************************************************** *********************
Usage Example:
$dbObject = new psqlDb;
if (
$dbObject->
dbLink(
"dbname=example")) {
// Connection is good
if (
$dbObject->
dbQuery("SELECT * FROM names WHERE firstname=
'Andrew')){
// Query Succeeded
print(
"<table>n"
print(
"<tr>n");
$innerLoops =
$dbObject->
dbGetNumFields();
do {
print(
"<td>" . $dbObject->dbGetFieldName($innerLoops) . "</td>");
}
while (
$counter <
$innerLoops);
print(</tr>n");
for (
$loopCounter =
0;
$loopCounter <
$dbObject->
dbNumRows();
$loopCounter++) {
$rowReturn =
$dbObject->
dbGetRow(
$loopCounter);
print(
"<tr>n");
for(
$iLoop =
0;
$iLoop <
$innerLoops;
$iLoop++) {
print(
"<td>" . $rowReturn($iLoop); . "</td>n");
}
print(
"</tr>n");
}
}
else {
// Query Failed... (either because of incorrect syntax or internal error)
// we'll only display output for the latter.
print(
"nError: " . $dbObject->dbGetErrors() . "!");
}
print(
"<table>");
$dbObject->
dbUnLink();
}
else if ((
$dbObject->
dbLink(
"dbname=example") == -
1) {
// Error connecting to db
print(
"nError: " . $dbObject->dbGetErrors() . "!");
}
else {
// dbLink() was not called properly
print(
"ndbLink() has not been called properlyn");
}
*/
cl
ass psqlDb{
/******************************************************************************* ********************
* Members *
******************************************************************************** *******************/
var
$connId; /* Datab
ase Connection ID */
var
$resultId; /* Datab
ase query
return ID */
var
$dbError; /* Keeps error status of pg_<
functions> */
/******************************************************************************* ********************
* Methods *
******************************************************************************** *******************/
function dbLink(
$dbConnString){
if (
isset(
$connId)) {
/* There's already a connection ID, so lets disconnect from the datab
ase and set the new
conn id */
$this->
dbUnLink();
}
if (
isset(
$dbConnString)) {
$conVar = @
pg_connect(
$dbConnString);
if (
$conVar) {
$this->connId =
$conVar;
return(
1);
}
else {
/* Uh oh! Error connecting to the db, so set
$dbError, and
return an error code */
$this->
__IsError();
return(-
1);
}
}
else {
/* This
function w
asn't called properly, set exit status -
2 */
return(-
2);
}
}
function dbUnlink(){
if (
isset(
$this->connId)){
pg_close(
$this->connId);
return(
1);
}
else {
/* The
function w
asn't called right, so we
return -
1 */
return(-
1);
}
}
function dbQuery(
$dbQueryString){
/* Send a query out to the datab
ase */
if (
isset(
$this->connId) &&
isset(
$dbQueryString)){
$returnVar = @
pg_exec(
$this->connId,
$dbQueryString);
if (
$returnVar){
$this->resultId =
$returnVar;
return(
1);
}
else {
/* The query didn't succeed
for some re
ason, so we set dbError and
return an error code */
$this->
__IsError();
return(-
1);
}
}
else {
return(-
1);
}
}
function dbNumRows() {
if (
isset(
$this->connId) &&
isset(
$this->resultId)) {
$numRowVar = @
pg_numrows(
$this->resultId);
if (
$numRowVar) {
return(
$numRowVar);
}
else {
/* Something happened, set dbError, and
return an error code */
$this->
__IsError();
return(-
1);
}
}
else {
/*
Return the standard
"function not called right" return code */
return(-
2);
}
}
function dbGetRow(
$row) {
if (
isset(
$this->connId) &&
isset(
$this->resultId) && (
$row <
$this->
dbNumRows())) {
$getRowVar = @
pg_fetch_array(
$this->resultId,
$row);
if (
$getRowVar) {
return(
$getRowVar);
}
else {
/* We seem to have an error
returning the number of rows */
$this->
__IsError();
return(-
1);
}
}
else {
/* No conn id, result id or
$row too big... set exit status -
2 */
return(-
2);
}
}
function dbGetNumFields() {
if (
isset(
$this->connId) &&
isset(
$this-> resultId)) {
$numFieldVar = @
pg_numfields(
$this->resultId);
if (
$numFieldVar) {
return(
$numFieldVar);
}
else {
/* Error due to some backend or connection problem, so we set dbError and continue */
$this->
__IsError();
return(-
1);
}
}
else {
/* Exit due to no conn id || result id */
return(-
2);
}
}
function dbGetFieldName(
$fieldNum) {
if (
isset(
$this->connId) &&
isset(
$this->resultId) && (
$fieldNum <=
$this->
dbGetNumFields())) {
$nameFieldVar = @
pg_fieldname(
$this->resultId,
$fieldNum);
if (
$nameFieldVar) {
return(
$nameFieldVar);
}
else {
/* Exit on field
return error, set the dbError var (not, we
don't have to check
for
a
return because this code only gets executed
if isset(
$this->connId) */
$this->
__IsError();
return(-
1);
}
}
else {
/* Exit due to no connection id or result id, or
$fieldnum, status -
2 */
return(-
2);
}
}
function dbGetFieldNum(
$fieldName) {
if (
isset(
$this->connId) &&
isset(
$this->resultId) && (
isset(
$fieldName))) {
$numFieldVar = @
pg_fieldname(
$this->resultId,
$fieldName);
if (
$numFieldVar) {
return(
$numFieldVar);
}
else {
/* Exit on field
return error, set the dbError var (not, we
don't have to check
for
a
return because this code only gets executed
if isset(
$this->connId) */
$this->
__IsError();
return(-
1);
}
}
else {
/* Exit due to no connection id or result id, or
$fieldName, status -
2 */
return(-
2);
}
}
function dbGetErrors() {
if (
isset(
$this->connId) &&
isset(
$this->dbError)) {
return(
$this->dbError);
}
}
function __IsError() {
if (
isset(
$this->connId)) {
$errorVar = @
pg_errormessage(
$this->connId);
if (
$errorVar) {
$this->dbError =
$errorVar;
}
else {
/* Exit on error
return failure, instead of
returning status, which would just make
this a pain, lets just set this error in
$dbError */
$this->dbError =
"psqlObj->__IsError() : Unable to acquire error status";
}
}
else {
/* Exit due to no connection id, status -
2 */
return(-
2);
}
}
}
?>
</body>
</html>
powrótTagi do źródła:
Funkcje do obsługi PostgreSQL, postgresql, sql, bazy danych, php
Nikt jeszcze nie skomentował tego źródła.