PHP
  Home arrow PHP arrow Page 5 - PHP and PostgreSQL
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP and PostgreSQL
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 43
    2002-05-01

    Table of Contents:
  • PHP and PostgreSQL
  • Getting Started
  • First Steps
  • Digging Deeper
  • Different Strokes
  • Rolling Around
  • Catching Mistakes
  • A Well-Formed Idea
  • Surfing The Web

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP and PostgreSQL - Different Strokes


    (Page 5 of 9 )

    Of course, there's more than one way of extracting records from a result set. The example on the previous page used an integer-indexed array; this one evolves it a little further so that the individual fields of each records are accessible as keys of a hash, or string-indexed array, via the pg_fetch_array() function.

    <html> <head><basefont face="Arial"></head> <body> <h2>Address Book</h2> <? // database access parameters // alter this as per your configuration $host = "localhost"; $user = "postgres"; $pass = "postgres"; $db = "test"; // open a connection to the database server $connection = pg_connect ("host=$host dbname=$db user=$user password=$pass"); if (!$connection) { die("Could not open connection to database server"); } // generate and execute a query $query = "SELECT name, address FROM addressbook ORDER BY name"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // get the number of rows in the resultset // this is PG-specific $rows = pg_num_rows($result); // if records present if ($rows > 0) { // iterate through resultset for ($i=0; $i<$rows; $i++) { $row = pg_fetch_array($result, $i, PGSQL_ASSOC); ?> <li><font size="-1"><b><? echo $row['name']; ?></b></font> <br> <font size="-1"><? echo $row['address']; ?></font> <p> <? } } // if no records present // display message else { ?> <font size="-1">No data available.</font> <? } // close database connection pg_close($connection); ?> </body> </html>
    Most of the magic here lies in the call to pg_fetch_array(),

    $row = pg_fetch_array($result, $i, PGSQL_ASSOC);
    which returns every row as a hash with keys corresponding to the column names.

    PHP also allows you to access individual fields within a row as object properties rather than array elements, via its pg_fetch_object() function. Take a look:

    <html> <head><basefont face="Arial"></head> <body> <h2>Address Book</h2> <? // database access parameters // alter this as per your configuration $host = "localhost"; $user = "postgres"; $pass = "postgres"; $db = "test"; // open a connection to the database server $connection = pg_connect ("host=$host dbname=$db user=$user password=$pass"); if (!$connection) { die("Could not open connection to database server"); } // generate and execute a query $query = "SELECT name, address FROM addressbook ORDER BY name"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // get the number of rows in the resultset // this is PG-specific $rows = pg_num_rows($result); // if records present if ($rows > 0) { // iterate through resultset for ($i=0; $i<$rows; $i++) { $row = pg_fetch_object($result, $i); ?> <li><font size="-1"><b><? echo $row->name; ?></b></font> <br> <font size="-1"><? echo $row->address; ?></font> <p> <? } } // if no records present // display message else { ?> <font size="-1">No data available.</font> <? } // close database connection pg_close($connection); ?> </body> </html>
    In this case, each row is returned as a PHP object, whose properties correspond to field names; these fields can be accessed using standard object notation.

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


       · Great article
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT