Why can't my script see this class?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

Why can't my script see this class?

Post by jeffrydell »

I'm trying to implement a database wrapper, but hit a snag in this code:

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>";
?>
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!
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Why can't my script see this class?

Post by alex.barylski »

Code: Select all

$Row = $db->query_first($sql);
Your $db object isn't available within the scope of the function, so either:

1. Bring the variable into function scope by doing:

Code: Select all

global $db; // Place this as the first line in your function
2. Pass the $db variable as a parameter to the method call like:

Code: Select all

 
function getName($type, $id, $db); // Pass the instance of the class as argument
 
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

Re: Why can't my script see this class?

Post by jeffrydell »

Hallelujah and Amen ... thank you!

Jeff
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Why can't my script see this class?

Post by php_east »

hi,

there are many ways to solve this. one of them ( probably easiest ) is to use
is to just pass your $db instance to function getName($type, $id)
like this...

function getName($type, $id, $db)
{
}

this error comes about simpy because function getName has no idea of the existance of $db,
yes scoping issue. there are other ways to solve this, but this one i provided is easiest and normal way.

you could also instantiate a new db inside function getName($type, $id, $db)
but that would be duplicate work as you already have the $db.

p/s oops PCSpectra typed in ealier. anyways, you get two answers now, we can't both go wrong :)
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

Re: Why can't my script see this class?

Post by jeffrydell »

Thanks for the help. I'm getting it now.

Much appreciated!
Post Reply