I created a page that has a PearDB connection called db. Later in my page, I need to access my database several different times based on a recordset I get from the database. I can pull the original recordset, but when I try to access db from inside my while loop, I get an error.
Here's the code:
$dsn = "$dbType://$dbUser:$dbPass@$dbHost/$dbName";
$db=DB::connect($dsn);
if (DB::isError($db))
{
die ($db->getMessage());
}
else {}
$db->setFetchMode(DB_FETCHMODE_OBJECT);
$sql = "select * from nametable";
$result = $db->query($sql);
//echo $result->numRows();
while ($row = $result->fetchRow())
{
echo "name: ".$row->Name."<br />";
foreach($theGrade as $key => $value)
{
//echo "Grade: ".$value."<br />";
$number = getResults($value,$row->Name);
//echo $number."<br />";
$theResults[$row->Name][$value][$number];
}
}
$tpl=& new HTML_Template_ITX('.');
$tpl->loadTemplatefile("templates/result2.tpl");
$tpl->setVariable("vote", "");
$tpl->show();
function getResults($grade,$name)
{
global $db;
$sql = "select * from votetable where Grade = '$grade' and Name = '$name'";
$res = $db->query($sql);
return $res->numRows();
//echo $count;
//return $count;
}
I get the error: Fatal error: Call to undefined function: numrows() in f:\www\msvote\result.php on line 60
That's the line return $res->numRows(); in my getResults function.
I'm pretty sure it's a scope problem, but I'm not sure how to fix it. Any help would be appreciated. Thanks!