Contents / Previous / Next


Error Handling

You man test the success of a SQL query by checking the returned result-resource: $query_str= "INSERT INTO stud (id, name, gender, diploma ) VALUES (17, 'sven', 'm', 'bio' );"; $res = pg_query( $dbh, $query_str ); if( $res==NULL ) { echo "<P>\$res==NULL<BR>"; exit(); } Or you can use one of the following functions:

string pg_result_error ( resource result):
Returns the error message associated with result resource.

string pg_last_error ( [resource connection]):
Returns the last error message for given connection.
Error messages may be overwritten by internal PostgreSQL(libpq) function calls. Hence this may not return appropriate error message, if multiple errors are occured inside a PostgreSQL module function.
Use pg_result_error(), pg_result_status() and pg_connection_status() for better error handling.

string pg_last_notice ( resource connection):
Returns the last notice message from the PostgreSQL server specified by connection. The PostgreSQL server sends notice messages in several cases, e.g. if the transactions cannot be continued.
With pg_last_notice(), you can avoid issuing useless queries, by checking whether the notice is related to the transaction or not.

Example:

$query_str= "INSERT INTO stud (id, name, gender, diploma ) VALUES (17, 'sven', 'm', 'bio' );"; $res = pg_query( $dbh, $query_str ); echo "query status: ".pg_result_status( $res )."<BR>"; echo "affected rows: ".pg_affected_rows( $res )."<BR>"; echo "result error: ".pg_result_error( $res )."<BR>"; echo "last error: ".pg_last_error( $dbh )."<BR>"; echo "last notice: ".pg_last_notice( $dbh )."<BR>";