| 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.
|