Example 8: Using PHP with PostgreSQL

Code:
<?
   $connection 
pg_connect("host=dbs.cs.utk.edu user=example 
                             password=password dbname=testdb"
);

   
$query "SELECT * FROM testtable";

   
/* The pg_exec function executes, on the connection represented by the 
      first argument, the query in the string given as the second argument.  
      The return value is an identifier pointing to the result set 
      for the query. */

   
$result pg_exec($connection$query);

   
/* The pg_numfields function returns the number of fields in a result set. */

   
print("There are ");
   print(
pg_numfields($result));
   print(
" fields in the testtable.<P>");

   
/* The pg_fieldname function returns the name of a field in a result 
      set based on the index of that field (returned fields are automatically 
      assigned indices from 0 to 1 less than the number of fields returned). */

   
print("The fields are: <BR>");

   for(
$i=0;$i<pg_numfields($result);$i++) {
      print(
pg_fieldname($result,$i));
      print(
"<BR>");
   }

   
/* The pg_numrows function returns the number of rows in a record set*/

   
print("The testtable has ");
   print(
pg_numrows($result));
   print(
" rows.<BR>");

   
/* The pg_fieldisnull function returns 1 if the specified field in 
      the specified row is NULL. */

   
if(pg_fieldisnull($result,0,0)) {
      print(
"The first field of the first row is null.<BR>");
   }
   else {
      print(
"The first field of the first row is not null.<BR>");
   }

   
/* The pg_fetch_array function returns an array containing each field 
      for the specified row indexed by both number and fieldname; note 
      that if you only need the fields indexed by number, you can use the 
      pg_fetch_row function with the same arguments */

   
$rowarray=pg_fetch_array($result,0);
   print(
"The first field of the first row contains: $rowarray[0]<BR>");

  
/* If you only need to find the value of a particular field in a 
     particular row, you can also use the pg_result function */

   
$fieldval pg_result($result,0,0);
   print(
"The first field of the first row still contains: ");
   print(
"$fieldval<BR>");

   
/* The pg_errormessage function returns the error message for the 
      last database access */
   
$errstring pg_errormessage($connection);

   if((
strlen($errstring))==0) {
      print(
"The call to pg_result was successful.<BR>");
      }
   else {
      print(
"Pg_result returned the following error message: $errstring<BR>");
      }

   
/* The pg_freeresult function frees the memory allocated for the 
      given result set. */
   
pg_freeresult($result);

   
/* The pg_close function closes the given connection. */
   
pg_close($connection);

?>

Output:

There are 3 fields in the testtable.

The fields are:
customer
name
state

The testtable has 3 rows.

The first field of the first row is not null.
The first field of the first row contains: 1234
The first field of the first row still contains: 1234
The call to pg_result was successful.

Click here to see the complete source code for this page