Jump to content

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


Cep

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.