Page 1 of 1

Help Using Pear DB

Posted: Fri Sep 22, 2006 7:21 pm
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?

Posted: Fri Sep 22, 2006 9:24 pm
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".

Posted: Fri Sep 22, 2006 9:58 pm
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.

Posted: Fri Sep 22, 2006 10:09 pm
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'];
}

Posted: Fri Sep 22, 2006 10:41 pm
by DanBlather
Thanks, that worked great!

Posted: Fri Sep 22, 2006 10:54 pm
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. ;) :)