PHPBuilder - PEAR Primer Page 4



RSS Twitter
Articles Pear

PEAR Primer - Page 4

by: Joe Stump
|
December 26, 2002

The PEAR DB Class

I could write two or three articles on the DB class included in PEAR, which is why I'll only briefly cover its use here. I use it flawlessly with MySQL and thus far my only complaint is that it uses sequence tables instead of merely returning the insert id in MySQL's auto_increment fields.

<?php

  
require_once('Base.php');

  
$db =& DB::connect(BASE_DEFAULT_DSN,true);
  if(!
DB::isError($db))
  {
    
$db->setFetchMode(DB_FETCHMODE_ASSOC);
    
    
$sql "SELECT *
            FROM table
            WHERE foo='bar'"
;

    
$result $db->query($sql);
    if(!
DB::isError($result) && $result->numRows())
    {
      while(
$row $result->fetchRow())
      {
        echo 
'&lt;li&gt;'.$row['foo']."\n";
      }
    }
  }
  else
  {
    echo 
$db->getMessage();
  }

?>
This is the most basic of examples on how to use the DB class, but those of you who have used Perl's DBI or the many PHP database functions will note the structure of connecting to a database is pretty much intact. One thing to note is that the result is a separate class from the DB class with its own functions, etc. For a more detailed overview of the DB class check out http://pear.php.net/manual/en/core.db.php.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Joe Stump

Comment:



Comment:

(Maximum characters: 1200). You have characters left.