MySQL
  Home arrow MySQL arrow Page 2 - PHP, MySQL and the PEAR Database
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
MYSQL

PHP, MySQL and the PEAR Database
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    PHP, MySQL and the PEAR Database - Rewriting the Books Example with PEAR
    (Page 2 of 4 )

    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 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway