Need help with PHP, MySQL and PEAR/MDB2

edited December 2008 in Tech
I'm trying to get up to speed with PHP/MySQL programming and am writing a test program using the MDB2 portion of the PEAR library to access a database. I'm using the following code:
<?php

$page_title = "Database Test"; // required by htmlhead.php

require_once("../htmlhead.php"); // Standard XHTML <head> section
require_once("../sqllogin.php"); // $dbtype, $sqlserver, $sqlusername, $sqlpassword, $dbname defined

require_once("MDB2.php"); // PEAR MDB2

$connection = MDB2::factory($dbtype.'://'.$sqlusername.':'.$sqlpassword.'@'.$sqlserver.'/'.$dbname);

if (PEAR::isError($connection)){
	die("<p>Could not select database \"" . $connection->getMessage() . "\".</p>");
	}

$sql = "SELECT * FROM licenses";
$licensetypes = $connection->query($sql);

if (PEAR::isError($licensetypes)){
	die('Could not query the database:<br />'. $licensetypes->getmessage());
	}

echo '<table>';

while ($license = $licencetypes->fetchRow(MDB2_FETCHMODE_ASSOC)) {
	echo '<tr><td>';
	echo $license[0] . '</td><td>';
	echo $license[1] . '</td></tr>';
	}
echo '</table>';

mysql_close();
require_once("../htmlfoot.php");
?>
based mostly on instructions found here but I'm getting the following error:
Fatal error: Call to a member function fetchRow() on a non-object in /home/diguana/phpsite/html/dbtest.php on line 25
The "licenses" table has two colums and currently has three rows of data entered, which display without a problem when I type "SELECT * FROM licenses;" from within the MySQL commandline utility. Anyone know what I'm doing wrong?

Alternatively, does anyone know of a good PHP/MySQL development forum where I can ask this sort of question?

Comments

  • edited December 2008
    I don't know PHP all that well, but when my father was learning it, he used this site for help :)
  • edited December 2008
    I dunno. I never really used the PEAR library. For the idiot simple stuff I've ever had to do using any library has always seemed like a bone-headed way to turn 2 lines of code into 20 for no damn good reason. I don't see how it makes sense to create an object to create another object to call a function that creates an object to call another function to access a flag that's read by another object to tell it to call a standard function that's built into the language that I could just call directly. It's not exactly like that, but it feels like it sometimes.

    I'll look at this tomorrow when I'm not so tired. (Connecting to a database and running a query can be done in just a few lines of code. I still want to know what advantage doing it with PEAR holds that makes it worth taking 30 lines to do the same thing, though.)
  • edited December 2008
    I'm mostly using it because the examples in the book I'm using for most of my reference uses it (although it's a bit outdated, using the DB libraries rather than MDB2.) Also, it adds a layer of abstraction between my code and the database, making it possible to switch to something other than MySQL with a minimum of code modification should I decide to do so.
  • edited December 2008
    Very well, I see it if you haven't found it yet.

    "$licencetypes" is not "$licensetypes". It's a just typo as is the case at least 90% of the time I think.
  • edited December 2008
    Note to self: Avoid giving variables names I've always had trouble spelling.