PHP
  Home arrow PHP arrow Database Techniques and PHP
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

Database Techniques and PHP
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-06-21


    Table of Contents:
  • Database Techniques and PHP
  • Issuing a Query
  • Inside a row array
  • Advanced Database Techniques

  • 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


    Database Techniques and PHP
    ( Page 1 of 4 )

    Picking up where we left off last week in our discussion of databases and PHP, we'll talk about connecting, issuing a query, and more. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

     Connecting 

    Once you have a DSN, create a connection to the database using the connect() method. This returns a database object you’ll use for tasks such as issuing queries and quoting parameters:

      $db = DB::connect(DSN [, options ]);

    The option value can either be Boolean, indicating whether or not the connection is to be persistent, or an array of options settings. The option values are given in Table 8-2.

    Table 8-2. Connection options

    Option Controls

    persistent

    Connection persists between accesses

    optimize

    What to optimize for
    debug Display debugging information

    By default, the connection is not persistent and no debugging information is displayed. Permitted values for optimize are 'performance' and 'portability' . The default is 'performance' . Here’s how to enable debugging and optimize for portability:

      $db = DB::connect($dsn, array('debug' => 1, 'optimize' => 'portability'));

    Error Checking

    PEAR DB methods return DB_ERROR if an error occurs. You can check for this with DB::isError():

      $db = DB::connect($datasource);
      if (DB::isError($db)) {
       
    die($db->getMessage());
      }

    The DB::isError() method returns true if an error occurred while working with the database object. If there was an error, the usual behavior is to stop the program and display the error message reported by the getMessage() method. You can call getMessage() on any PEAR DB object.



     
     
    >>> More PHP Articles          >>> More By O'Reilly Media
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - 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





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