Article

Beginning Databases with PostgreSQL - Chapter 15: Accessing PostgreSQL from PHP

Page: 1 2 3 4 5 6 7 8 9 10 11 12 Next

Error Handling

We touched on error handling very briefly in an earlier section. We will now cover it in a bit more detail.

Nearly all PostgreSQL related functions return some sort of predictable value upon an error (generally false or -1). This makes it fairly easy to detect error situations so that your script can fail gracefully. For example:

$db_handle = pg_connect('dbname=bpsimple');          
if (!$db_handle) {          
   header("Location: http://www.example.com/error.php");          
   exit;          
}

In the above example, the user was to be redirected to an error page if the database connection attempt were to fail.

pg_errormessage()

pg_errormessage() can be used to retrieve the text of the actual error message as returned by the database server. pg_errormessage() will always return the text of the last error message generated by the server. Be sure to take that into consideration when designing your error handling and display logic.

You will find that, depending on your level of error reporting, PHP can be fairly verbose when an error occurs, often outputting several lines of errors and warnings. In a production environment, it is often undesirable to display this type of message to the end user.

The @ Symbol

The most direct solution is to lower the level of error reporting in PHP (controlled via the error_reporting configuration variable in the php.ini). The second option is to suppress these error messages directly from PHP code on a per-function-call basis. PHP uses the @ symbol to indicate error suppression. For example, no errors will be output from the following code:

$db_handle = pg_connect("host=nonexistent_host");          
$result = @pg_exec($db_handle, "SELECT * FROM item");

Without the @ symbol, the second line above would generate an error complaining about the lack of a valid database connection (assuming your error reporting level was high enough to cause that error to be displayed, of course).

Note that the above error could still be detected by testing the value of $result, though, so suppressing the error message output doesn't preclude our dealing with error situations programmatically. Furthermore, we could display the error message at our convenience using the pg_errormessage() function.

Character Encoding

If character encoding support is enabled in PostgreSQL, PHP provides functions for getting and setting the current client encoding. By default, the encoding is set to SQL ASCII.

The supported character sets are: SQL_ASCII, EUC_JP, EUC_CN, EUC_KR, EUC_TW, UNICODE, MULE_INTERNAL, LATINX (X=1...9), KOI8, WIN, ALT, SJIS, BIG5, WIN1250.

pg_client_encoding()

The pg_client_encoding() function will return the current client encoding:

$encoding = pg_client_encoding($db_handle);

pg_set_client_encoding()

You can set the current client encoding using the pg_set_client_encoding() function:

pg_set_client_encoding($db_handle, 'UNICODE');

If you liked this article, share the love:
Print-Friendly Version Suggest an Article