Home arrow MySQL arrow Page 2 - PHP, MySQL and the PEAR Database

Rewriting the Books Example with PEAR - MySQL

In this conclusion to a three-part series, you will add PEAR into the mix of what you have already learned. This article is excerpted from chapter 9 of Learning PHP and MySQL, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Copyright 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. PHP, MySQL and the PEAR Database
  2. Rewriting the Books Example with PEAR
  3. Creating a connect instance
  4. PEAR error reporting
By: O'Reilly Media
Rating: starstarstarstarstar / 8
May 31, 2007

print this article



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
3 include('db_login.php');
4 require_once('DB.php');
6 $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
8 if (DB::isError($connection)){
9 die("Could not connect to the database:
<br />".DB::errorMessage($connection)); 10 }
12 $query = "SELECT * FROM `books` NATURAL JOIN `authors`";
13 $result = $connection->query($query);
15 if (DB::isError($result)){
16 die("Could not query the database:
<br />".$query." ".DB::errorMessage($result));
17 }
19 echo('<table border="1">');
20 echo '<tr><th>Title</th><th>Author</th><th>Pages </th></tr>';
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 }
29 echo("</table>");
30 $connection->disconnect();
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:


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.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: