FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


need help: cant call $db object from within my function





ammonkc
Code:
PHP Fatal error:  Call to a member function query() on a non-object in /Users/ammonkc/Sites/it_req/gantt.php on line 11

I keep getting this error when I try to call my db object. I'm connecting to the db with pear DB. my db object works fine unless I try to use it in a function. it says that I'm trying to call a non-object. I know its a OOP problem. but I don't what it is. What is the proper way to use an object inside of a function like this?

here is a snippet of the code:
Code:

<?php
/* 2006-10-06
 * Builds a GANTT chart of the project
 */
require_once( 'opendb.php' );
$progress = get_progress($_GET['req_id']);


function get_progress ($id) {
   $get_prog_sql = "SELECT * FROM progress WHERE req_id = '".$id."' ORDER BY step_num ASC";
   $result = $db_object->query($get_prog_sql);
   if ( DB::isError($result) || $result->numRows() == 0 ) {
      return getMessage($result);
   }else{
      return $result;
   }//END if db isError
}//END function
?>
hexkid
ammonkc wrote:
I keep getting this error when I try to call my db object.

I don't do OOP, but I don't see the definition of $db_object anywhere ...
Maybe you need to declare it global?

Code:
// ...
function get_progress ($id) {
   global $db_object; ### use global $db_object???
   $get_prog_sql = "SELECT * FROM progress WHERE req_id = '".$id."' ORDER BY step_num ASC";
// ...
ammonkc
hexkid wrote:
ammonkc wrote:
I keep getting this error when I try to call my db object.

I don't do OOP, but I don't see the definition of $db_object anywhere ...
Maybe you need to declare it global?


actually the $db_object is declared in the opendb.php file. that include file has the connection script in it. it works fine as long as I don't try to connect to the db from inside a function.
rohan2kool
make this the first line in your function:

Code:

function get_progress($id) {
   global $db_object;

   //Rest of the function here
}


most probably this should work.
Related topics
SQL Tutorial
PHP DB Error: syntax error
Fashionable Nonsense by Alan Sokal and Jean Bricmont
[Resolved] Object Orientated PHP errors
phpbb cash mod problemos
PHP VS ASP
Phpbb and PhpNuke Help
Password protect pages with phpbb
Error on parsing smily pack, PhpBB
Error on php-nuke install, need assistance.
need help: Call to undefined function error
[Help] Problem installing phpBB2
help!!! cant access cpanel or my site!!
Outsourcing your help desk call center to PH.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.