Help Using Pear DB

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
DanBlather
Forum Newbie
Posts: 7
Joined: Fri Sep 22, 2006 6:53 pm

Help Using Pear DB

Post by DanBlather »

I'm writing my first PHP program using the O'Reilly "Learning PHP 5" book. I am trying to use the Pear DB functions and getting the following errors:

Notice: Only variable references should be returned by reference in /usr/lib/php/DB/common.php on line 501
Fatal error: Call to undefined function: numrows() in /home/jdvetcom/public_html/qEventsPHP.php on line 17

The PHP part of my code is below:

Code: Select all

<?php
     error_reporting(E_ALL); 
     require_once 'DB.php';

     $db = DB::connect("mysql://xxx:yyy@localhost/zzz");
     if (DB::isError($db)) {die("can't connect: " . $db->getMessage());}

     $q = $db->query("SELECT * FROM event");

     print "there are " . $q->numrows() . " in event table";
    ?>
The fact that numrows() is undefined make me wonder if the Pear DB functions are supported on my server. My questions are: did I code this wrong? Is there an easy way to see if Pear DB is installed?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Please put PHP tags around code that you post (it's a button in the post editor).

The notice is because PEAR DB is no longer supported and the code has not been changed to fix changes in references in recent versions of PHP.

The error is because the method name is "numRows" not "numrows".
(#10850)
DanBlather
Forum Newbie
Posts: 7
Joined: Fri Sep 22, 2006 6:53 pm

Post by DanBlather »

arborint wrote:Please put PHP tags around code that you post (it's a button in the post editor).
Will do.
The notice is because PEAR DB is no longer supported and the code has not been changed to fix changes in references in recent versions of PHP.
Is there a better alternative to PEAR DB that I should be using?
The error is because the method name is "numRows" not "numrows".
I changed the code and got same error. I thought PHP was not case sensitive.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

pear is good as an abstraction layer but not necessary to connect to a mysql database. In fact, I think you'd do better (from a learning perspective) to just use the mysql functions. Once you have a handle on them, you could write your own database abstraction layer.

here's a short sample of using the mysql functions:

Code: Select all

mysql_connect("localhost","username","password")
    or die(mysql_error());
mysql_select_db("somedatabase")
    or die(mysql_error());

$result = mysql_query("SELECT * FROM `sometable` WHERE `somefield` = 1")
    or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
    echo $row['someotherfield'];
}
DanBlather
Forum Newbie
Posts: 7
Joined: Fri Sep 22, 2006 6:53 pm

Post by DanBlather »

Thanks, that worked great!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Your next project is to create basic Connection/Query and RecordSet classes. You can search these forums for examples. We expect a full report and code in Code Critique. ;) :)
(#10850)
Post Reply