Jump to content


Photo

Call to a member function on a non-object in on line 5


  • Please log in to reply
3 replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 29 March 2006 - 07:24 PM

I am trying calling a function from another script but everytime I run it, I get the following error.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Fatal error: Call to a member function on a non-object in /wbbrpg_functions.php on line 5
[/quote]

The call is,

$n_tile = rpg_gettile($nid);

Where $nid is an integer.

and the function that causes the error is,

function rpg_gettile($tile_id) {

         $type = $db->query_first("SELECT * FROM bb1_wbbrpg_location_types WHERE typeid = ".$tile_id."");

         $tile_name = $type['name'];
         $tile_img = $type['image'];
         $tile_magic = $type['magic'];
         $tile_fly = $type['fly'];
         $tile_walk = $type['walk'];
         $tile_sail = $type['sail'];
         $tile_tunnel = $type['tunnel'];

         $tile_array = array($tile_id, $tile_name, $tile_img, $tile_magic, $tile_fly, $tile_walk, $tile_sail, $tile_tunnel);

         return $tile_array;
}

Line 5 is the SQL statement
Thanks Cep

#2 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 29 March 2006 - 07:30 PM

the error is probably: $db->...

is the object $db declared somewhere previously using the new command?

is $db out of the scope of how your using it?
you might have to write this within you function "rpg_gettile"

global $db;


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 March 2006 - 07:36 PM

variables inside a function are "local" to that function. $db is not defined inside the function and has a null value.

If you want to use $db inside the function either

- declare it as global in the function

function rpg_gettile($nid) {

     global $db;

     ## rest of function code
}

or, better, pass it an an argument to the function

function rpg_gettile($db, $nid) {

     ## rest of function code
}

$n_tile = rpg_gettile($db, $nid); // pass $db to function

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 29 March 2006 - 07:47 PM

Thanks guys I didnt spot that I had but that there :) cheers!
Thanks Cep




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users