PHP
  Home arrow PHP arrow Page 3 - PHP and PostgreSQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 44
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - First Steps
    ( Page 3 of 9 )

    Now, how about doing the same thing with PHP - fire a SELECT query at the database, and display the results in an HTML page?

    <html> <head><basefont face="Arial"></head> <body> <? // 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 * FROM addressbook"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // get the number of rows in the resultset $rows = pg_num_rows($result); echo "There are currently $rows records in the database."; // close database connection pg_close($connection); ?> </body> </html>
    And here's what's the output looks like:

    There are currently 3 records in the database.
    As you can see, using PHP to get data from a PostgreSQL database involves several steps, each of which is actually a pre-defined PHP function. Let's dissect each step:

    1. The first thing to do is specify some important information needed to establish a connection to the database server. This information includes the server name, the username and password required to gain access to it, and the name of the database to query. These values are all set up in regular PHP variables.

    // database access parameters // alter this as per your configuration $host = "localhost"; $user = "postgres"; $pass = "postgres"; $db = "test";
    2. In order to begin communication with the PostgreSQL database server, you first need to open a connection to the server. All communication between PHP and the database server takes place through this connection.

    In order to initialize this connection, PHP offers the pg_connect() function.

    // open a connection to the database server $connection = pg_connect ("host=$host dbname=$db user=$user password=$pass");
    The function requires a connection string containing one or more parameters - these could include the host name, port, database name, user name and user password. Here are some examples of valid connection strings:

    $connection = pg_connect ("host=myhost dbname=mydb"); $connection = pg_connect ("host=myhost dbname=mydb user=postgres password-postgres"); $connection = pg_connect ("host=myhost port=5432 dbname=mydb user=postgres password-postgres");
    This function then returns a "link identifier", which is stored in the variable $connection; this identifier is used throughout the script when communicating with the database.

    3. Now that you have a connection to the database, it's time to send it a query via the pg_query() function. This function needs two parameters: the link identifier for the connection and the query string.

    // generate and execute a query $query = "SELECT * FROM addressbook"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection));
    The result set returned by the function above is stored in the variable $result.

    4. This result set may contain, depending on your query, one or more rows or columns of data. You then need to retrieve specific sections or subsets of the result set with different PHP functions - the one used here is the pg_num_rows() function, which counts the number of rows and returns the value needed.

    // get the number of rows in the resultset $rows = pg_num_rows($result);
    There are several other alternatives you can use at this point, which will be explained a little further down.

    5. Finally, each database connection occupies some amount of memory - and if your system is likely to experience heavy load, it's a good idea to use the pg_close() function to close the connection and free up the used memory.

    // close database connection pg_close($connection);
    Simple, ain't it?

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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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