PHP
  Home arrow PHP arrow Page 8 - 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? 
Google.com  
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 - A Well-Formed Idea
    ( Page 8 of 9 )

    Finally, how about one more example to wrap things up? This next script contains a form which can be used to enter new addresses into the table, together with a form processor that actually creates and executes the INSERT statement.

    <html> <head><basefont face="Arial"></head> <body> <h2>Address Book</h2> <? // form not yet submitted // display form if (!$submit) { ?> <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST"> Name:<br> <input name="name" type="text" size="50"> <p> Address:<br> <textarea name="address" rows="6" cols="40"></textarea> <p> Tel:<br> <input name="tel" type="text" size="10"> <p> Email:<br> <input name="email" type="text" size="30"> <p> <input type="submit" name="submit" value="Add"> </form> <? } else { // form submitted // prepare to insert data // 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"); } // error checks on form submission go here // generate and execute a query $query = "INSERT INTO addressbook VALUES (nextval('addressbook_id_seq'), '$name', '$address', '$tel', '$email')"; $result = pg_query($connection, $query) or die("Error in query: $query. " . pg_last_error($connection)); echo "Data successfully added."; // close database connection pg_close($connection); } ?> </body> </html>
    As you can see, this script is broken up into two main sections - the appropriate section is displayed depending on whether or not the form has been submitted.

    The first part merely displays an HTML form, with fields corresponding to the columns in the "addressbook" table. Once the user enters data into these fields, the same script is called again; this time, the second half will get executed. An SQL query is generated from the data entered into the form, and this query is executed using the pg_query() function you've become familiar with. A success message is displayed once the data has been successfully INSERTed.

    If you're familiar with building MySQL-based Web applications with PHP, the procedure above should be familiar to you - the only difference lies in the functions used to communicate with the database server.

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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek