PHP
  Home arrow PHP arrow Page 5 - Databases: Finishing a Listing Service
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

Databases: Finishing a Listing Service
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-07-05


    Table of Contents:
  • Databases: Finishing a Listing Service
  • Adding a Business
  • Displaying the Database
  • PHP Data Objects
  • PDO and prepared statements

  • 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


    Databases: Finishing a Listing Service - PDO and prepared statements
    ( Page 5 of 5 )

    PDO also allows for what is known as prepared statements. This is done with PDO calls in stages or steps. Consider the following code:

      $stmt = $ConnHandle->prepare( "SELECT * FROM books");
      $stmt->execute();

      while ($row = $stmt->fetch()) {   // gets rows one at a time
         
    print_r ($row);
         
    // or do something more meaningful with each returned row
     
    }
      $stmt = null;

    In this code, we “prepare” the SQL code then “execute” it. Next, we cycle through the result with the while code and, finally, we release the result object by assigning null to it. This may not look all that powerful in this simple example, but there are other features that can be used with prepared statements. Now, consider this code:

      $stmt = $db->prepare("INSERT INTO authors"
         
    . "(authorid, title, ISBN, pub_year)"
         
    . "VALUES (:authorid, :title, :ISBN, :pub_year)");
      $stmt->execute(array('authorid'  => 4,
                          
    'title'     => 'Foundation',
                          
    'ISBN'      => 0-553-80371-9,
                          
    'pub_year' => 1951)
      );

    Here, we prepare the SQL statement with four named placeholders: authorid , title , ISBN ,  and pub_year . These happen to be the same names as the columns in the database. This is done only for clarity; the placeholder names can be anything that is meaningful to you. In the execute call, we replace these placeholders with the actual data that we want to use in this particular query. One of the advantages of prepared statements is that you can execute the same SQL command and pass in different values through the array each time. You can also do this type of statement preparation with positional placeholders (not actually naming them), signified by a ? , which is the positional item to be replaced. Look at the following variation of the previous the code:

      $stmt = $db->prepare("INSERT INTO authors"
         
    . "(authorid, title, ISBN, pub_year)"
         
    . "VALUES (?,?,?,?)");

      $stmt->execute(array(4, 'Foundation', 0-553-80371-9, 1951));

    This code accomplishes the same thing but with less code, as the value area of the SQL statement does not name the elements to be replaced, and, therefore, the array in the execute statement only needs to send in the raw data and no names. You just have to be sure about the position of the data that you are sending into the prepared statement.

    This was just a brief overview of what the new PDO library will be able to do for you in the database realm of PHP. If you want to explore this new library in more depth, be sure to do your research and testing before using it in a production environment. You can find information on PDO at http://ca.php.net/pdo.



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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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