PHP
  Home arrow PHP arrow Page 7 - Database Abstraction With 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? 
PHP

Database Abstraction With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 57
    2002-02-13


    Table of Contents:
  • Database Abstraction With PHP
  • Alphabet Soup
  • Sultans Of Swing
  • Independence Day
  • Different Strokes
  • The Number Game
  • Preparing For The Long Haul
  • Commitment Issues
  • No News Is Good News
  • Catch Me If You Can
  • Once Again, The Headlines

  • 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 Abstraction With PHP - Preparing For The Long Haul
    ( Page 7 of 11 )

    In the event that you need to execute a particular query multiple times with different values - for example, a series of INSERT statements - the DB class comes with two methods that can save you a huge amount of time (see, there's that lazy thing again!) and also reduce overhead. Consider the following example, which demonstrates:

    <?php // include the DB abstraction layer include("DB.php"); // set up array of track titles $tracks = array("Track A", "Track B", "Track C", "Track D"); // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // prepare query $query = $dbh->prepare("INSERT INTO tracks (cd, track) VALUES (12, ?)"); // execute query // run as many times as there are elements in $tracks foreach($tracks as $t) { $result = $dbh->execute($query, $t); } // close database connection $dbh->disconnect(); ?>
    The prepare() function, which takes an SQL query as parameter, readies a query for execution, but does not execute it (kinda like the priest that walks down the last mile with you to the electric chair). Instead, prepare() returns a handle to the prepared query, which is stored and then passed to the execute() method, which actually executes the query (bzzzt!).

    Note the placeholder used in the query string passed to prepare() - this placeholder is replaced by an actual value each time execute() runs on the prepared statement. The second argument to execute() contains the values to be substituted in the query string.

    In the event that you have multiple placeholders within the same query string, you can pass execute() an array of values instead of a single value - as demonstrated below:

    <?php // include the DB abstraction layer include("DB.php"); // set up array of CD titles $cds = array(); // each element of this array is itself an array $cds[] = array("CD A", "Artist A"); $cds[] = array("CD B", "Artist B"); $cds[] = array("CD C", "Artist C"); // connect to the database $dbh = DB::connect("mysql://john:doe@localhost/db287"); // prepare query $query = $dbh->prepare("INSERT INTO cds (title, artist) VALUES (?, ?)"); // execute query // run as many times as there are elements in $cds foreach($cds as $c) { $result = $dbh->execute($query, $c); } // close database connection $dbh->disconnect(); ?>
    Although overkill for our simple needs, you should be aware that prepare() can also read data directly from a file, rather than an array. I'll leave this to you to experiment with.

     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    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