PHP
  Home arrow PHP arrow Page 6 - 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 
Dedicated Servers 
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 / 42
    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 - Rolling Around


    (Page 6 of 9 )

    One of the nice things about PostgreSQL - and one of the reasons why many developers prefer it over MySQL - is its support for transactions (in case you didn't know, this refers to the ability to group a series of SQL statements together so that they are executed either together, or not at all). You can find more information about transactions online, at http://www.postgresql.org/idocs/index.php?tutorial-transactions.html - and if you already know what they are, here's an example which demonstrates how they may be used in a PHP context with PostgreSQL.

    <? // 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"); } // begin a transaction block $query = "BEGIN WORK"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // generate some queries $query = "INSERT INTO addressbook values (nextval('addressbook_id_seq'), 'Spiderman', 'The Web, Somewhere In Your Neighborhood', 'None', 'spidey@neigborhood.com')"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); $query = "INSERT INTO addressbook values (nextval('addressbook_id_seq'), 'Bruce Wayne', 'Gotham City', '64928 34585', 'bruce@batcave.org')"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // now roll them back $query = "ROLLBACK"; // if you want to commit them, comment out the line above // and uncomment the one below // $query = "COMMIT"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); // now check to see how many records are there in the table // and print this $query = "SELECT * FROM addressbook"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); $rows = pg_num_rows($result); echo "There are currently $rows records in the database"; // close database connection pg_close($connection); ?>
    Technically, there's nothing new here - this script uses the same functions you've seen in preceding examples. The difference lies in the use of multiple SQL statements to begin and end a transaction block, and in the use of COMMIT and ROLLBACK statements to commit and erase records from the database.

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


       · Great article
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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