Home arrow PHP arrow Page 3 - Databases and PHP

PEAR DB Basics - PHP

If you're interested in learning how to access databases from PHP, you've come to the right place. This four-part series will focus on the PEAR DB system. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Databases and PHP
  2. Relational Databases and SQL
  3. PEAR DB Basics
  4. Data Source Names
By: O'Reilly Media
Rating: starstarstarstarstar / 8
June 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

Example 8-1.  Display book information

<html><head><title>Library Books</title></head>
<body>

<table border=1>
<tr><th>Book</th><th>Year Published</th><th>Author</th></tr>
<?php
 // connect
 
require_once('DB.php');
 
$db = DB::connect("mysql://librarian:passw0rd@localhost/ library");
 
if (DB::iserror($db)) {
  
die($db->getMessage());
 }

 // 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());
 }

 // generate the table
 
while ($q->fetchInto($row)) {
?>
<tr><td><?= $row[0] ?></td>
    <td><?= $row[1] ?></td>
   
<td><?= $row[2] ?></td>
</tr>
<?php
 
}
?>

The output of Example 8-1 is shown in Figure 8-1.


Figure 8-1.  The Library page



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

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: