RE: [PEAR] Fatal error when calling DB in function

From: Date: Wed, 05 Dec 2007 05:14:20 +0000
Subject: RE: [PEAR] Fatal error when calling DB in function
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-28267@lists.php.net to get a copy of this message
You need to either pass the $db object into your function or declare the db object as global: Example 1 function getProps( $db ) { } Example 2 function getProps() { global $db; } -----Original Message----- From: batterhead [mailto:batterhead@gmail.com] Sent: Wednesday, December 05, 2007 7:00 AM To: pear-general@lists.php.net Subject: [PEAR] Fatal error when calling DB in function Hi everyone, I have a bit of code which to me, looks like it should work, but is not and I am not sure why. Maybe someone here could help me. If I process this query outside of a function it works just fine. If I call it using a function as show below I get the error: Fatal error: Call to a member function on a non-object in /Library/WebServer/... <?php require_once("include/dbConnect.inc.php"); function getProps() { $res = $db->query('SELECT * FROM properties'); while ($res->fetchInto($prop)) { echo $prop->id . "<br />"; } $res->free(); } getProps(); ?> -- PEAR General Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.pear.general (#28267) next »