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
<?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?
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:
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'];
}
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.