Jump to content

Archived

This topic is now archived and is closed to further replies.

Cep

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

Recommended Posts

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,

[code]
$n_tile = rpg_gettile($nid);
[/code]

Where $nid is an integer.

and the function that causes the error is,

[code]
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;
}
[/code]

Line 5 is the SQL statement

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

[code]function rpg_gettile($nid) {

     global $db;

     ## rest of function code
}[/code]

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

[code]function rpg_gettile($db, $nid) {

     ## rest of function code
}

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

Share this post


Link to post
Share on other sites
Thanks guys I didnt spot that I had but that there :) cheers!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.