Pear::DB issues

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
scrypte
Forum Newbie
Posts: 6
Joined: Sat Nov 06, 2004 3:31 pm

Pear::DB issues

Post by scrypte »

here is the connection:

Code: Select all

/*
Database Connection | Pear::DB API
--------------------------------------------------------------*/
$dsn = "mysql://$user:$pass@$host/$db_name";

$db = DB::connect($dsn);

if (DB::isError($db)) 
{
        die ($db->getMessage());
}

here is the code:

Code: Select all

/**
 * Creates the header for the directory
 *
 * @param string $hostname
 * @param string $view
 * @param string $i
 * @return string
 */
 function top_nav($hostname,$view, $i) 
 {
 		$l = chr($i);
 		
 		// Connect to the database so that 
 		$s = "SELECT * FROM md_models WHERE alias REGEXP '^($l)'";
 		$q = $db->query($s);
 		
 		if (DB::isError($result)) 
 		{
        	die ($result->getMessage());
		}
 		
		$r = $q->fetchRow();		
		$v = $db->getOne($s);
		
 
		// Print valid letters
		if ($v!=0)
		{
 			$letter = '<td  width=20 align=center>
 			<a href="index.php?cmd=find&l='.$l.'&view='.$view.'" 
 			style="color: black; text-decoration: none; font-weight: bold">'.$l.'</a>
 			</td>';
		 }
		 else
 		{
 			$letter = '<td  width=20 align=center style="color: #CCCCCC; font-weight: bold">'.$l.'</td>';
 		}

	return $letter;

}
This is the error:
Fatal error: Call to a member function on a non-object in /data1/members.scoreland/docs/cgi/secured/modeldir/lib/functions/functions.misc_utilities.php on line 77

Here is line 77:

Code: Select all

 		$q = $db->query($s);
Does anyone have any clue what is causing the issue, its starting to annoy me.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

your $db object has to be available to the code inside of the function
You could either pass the $db object to a function as parameter,

Code: Select all

function top_nav($db, $hostname,$view, $i)
or

access it via global

Code: Select all

function top_nav($hostname,$view, $i) {
global $db;
.
.
.
}
Post Reply