MySQL
  Home arrow MySQL arrow Page 2 - PHP, MySQL and the PEAR Database
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  
MYSQL

PHP, MySQL and the PEAR Database
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-05-31


    Table of Contents:
  • PHP, MySQL and the PEAR Database
  • Rewriting the Books Example with PEAR
  • Creating a connect instance
  • PEAR error reporting

  • 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


    When using the PEAR DB package, you follow the same steps. However, the function syntax is slightly different. We'll go line by line and explain the differences as they appear in Example 9-7.

    Example 9-7.  Displaying the books table with PEAR DB

    1 <?php
    2
    3 include('db_login.php');
    4 require_once('DB.php');
    5
    6 $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
    7
    8 if (DB::isError($connection)){
    9 die("Could not connect to the database:
    <br />".DB::errorMessage($connection)); 10 }
    11
    12 $query = "SELECT * FROM `books` NATURAL JOIN `authors`";
    13 $result = $connection->query($query);
    14
    15 if (DB::isError($result)){
    16 die("Could not query the database:
    <br />".$query." ".DB::errorMessage($result));
    17 }
    18
    19 echo('<table border="1">');
    20 echo '<tr><th>Title</th><th>Author</th><th>Pages </th></tr>';
    21
    22 while ($result_row = $result->fetchRow()) {
    23 echo "<tr><td>";
    24 echo $result_row[1] . '</td><td>';
    25 echo $result_row[4] . '</td><td>';
    26 echo $result_row[2] . '</td></tr>';
    27 }
    28
    29 echo("</table>");
    30 $connection->disconnect();
    31
    32 ?>
     

    Example 9-7 displays the screen shown in Figure 9-7.

    Figure 9-7.  Switching to the PEAR DB functions didn't change the output

    Notice that Figure 9-7 is identical to the output in Figure 9-4 .

    Line 3 includes your database login information and remains unchanged:

      include('db_login.php');

    Line 4 has a new require statement:

      require_once( "DB.php" );

    This requires the file DB.php, which provides the PEAR DB functions. The require_once function errors out if the DB.php file is not found. It also will not include the file if it has been incorporated already. And, this would cause an error.

    The file DB.php is found in the /pear subdirectory of the PHP distribution. The PEAR install should have added that directory to the include_path in the php.ini file. If this file is not found, verify that PEAR DB is installed and that the paths are set up correctly.

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





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