Example 8-1 is a program to build an HTML table of information about science fiction books. It demonstrates how to use the PEAR DB library (which comes with PHP) to connect to a database, issue queries, check for errors, and transform the results of queries into HTML. Be sure to verify how to turn on PEAR with your setup of PHP, as the Unix/Linux flavor is slightly different to that of Windows. Go to http:// www.pear.php.net/manual/en/installation.getting.php for a starting point on installation. The library is object-oriented, with a mixture of class methods (DB::connect(), DB::iserror()) and object methods ($db->query(),$q->fetchInto()).
// issue the query $sql = "SELECT books.title,books.pub_year,authors.name FROM books, authors WHERE books.authorid=authors.authorid ORDER BY books.pub_year ASC"; $q = $db->query($sql); if (DB::iserror($q)) { die($q->getMessage()); }