Why can't my script see this class?
Posted: Fri Apr 17, 2009 11:55 am
I'm trying to implement a database wrapper, but hit a snag in this code:
I can show the 'events' in the 'while()' loop just fine without the getName() ... but when I implement that function, it throws these errors:
Notice: Undefined variable: db in /var/www/vhosts/agilityevents.net/subdomains/sandbox/httpdocs/dbtest.php on line 28
Fatal error: Call to a member function query_first() on a non-object in /var/www/vhosts/agilityevents.net/subdomains/sandbox/httpdocs/dbtest.php on line 28
Apparently the code in the function can't 'see' the database wrapper class. Why not? More importantly, what can I do so that class is 'available' within the function?
Thanks for any help you can offer. Still struggling with OOP. Argh!
Code: Select all
<?php
function getName($type, $id)
{
// Figgr out what we're looking up & set vars for query
switch ($type)
{
case "c": // Club
$table = "clubs";
$field = "club_id";
$rtn = "name";
break;
case "s": // Secretary
$table = "secretaries";
$field = "sec_id";
$rtn = "name";
break;
case "v": // Venue
$table = "venues";
$field = "venue_id";
$rtn = "name";
break;
default:
return "";
}
// Get the value to return via the Database class
$sql = "SELECT `$rtn` FROM `$table` WHERE `$field` = " . $id ;
$Row = $db->query_first($sql);
return $Row[$rtn];
}
// Development stuff (show errors)
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Database class files
require_once('includes/config.inc.php');
require_once('includes/Database.class.php');
// create the $db ojbect
$db = new Database($config['server'], $config['user'], $config['pass'], $config['database'], $config['tablePrefix']);
// connect to the server
$db->connect();
$sql = "SELECT `event_id`, `venue_id`, `club_id`, `sec_id` FROM `events` WHERE `entry_url` IS NOT NULL AND `close_date` > NOW() AND `open_date` < NOW() ORDER BY `start_date`";
$Rows = $db->query($sql);
echo "<table><tbody>";
while ($Row = $db->fetch_array($Rows)) {
echo "<tr><td>$Row[event_id]</td><td>" . getName("v", $Row['venue_id']) . "</td><td>$Row[club_id]</td><td>$Row[sec_id]</td></tr>";
}
echo "</tbody></table>";
?>Notice: Undefined variable: db in /var/www/vhosts/agilityevents.net/subdomains/sandbox/httpdocs/dbtest.php on line 28
Fatal error: Call to a member function query_first() on a non-object in /var/www/vhosts/agilityevents.net/subdomains/sandbox/httpdocs/dbtest.php on line 28
Apparently the code in the function can't 'see' the database wrapper class. Why not? More importantly, what can I do so that class is 'available' within the function?
Thanks for any help you can offer. Still struggling with OOP. Argh!