Jump to content

Why can't I run $q = $conn->query($sql); in a function?


Recommended Posts

:suicide:  :confused:  :suicide:

for some reason I'm getting this error heavily:

"Fatal error: Call to a member function query() on a non-object in..."

 

All the function does is:
 

function grab_function($functionName, $language){
global $conn;
$sql = "SELECT * FROM  `functions`
        WHERE  `language` =  '$language'
        AND  `name` =  '$functionName'
        LIMIT 1";
        $q = $conn->query($sql);
        $q->setFetchMode(PDO::FETCH_ASSOC);
        $result = $q->fetch();
    $source = $result['source'];
    eval($source);
}

Where's the bug at?

It sounds like $conn isn't pointing to your database object. Did you open a database connection before calling grab_function()? Was the database object assigned to $conn.

I'm giving my function permission to go out, and grab the variable $conn, and take it home right?

 

specific line is:         $q = $conn->query($sql);

Edited by Q695

I'm giving my function permission to go out, and grab the variable $conn, and take it home right?

 

Your function does declare $conn as global. So it should grant the function access to the database object.

 

However, the error says that $conn isn't an object. That leads me to believe that the database connection wasn't made before calling the function. Or the database object wasn't assigned to $conn. Maybe it's being stored in $con, $dbs, or something else.

Should I switch to mysqli to try to solve it that way?

 

You would still be dealing with a database object, so it shouldn't make a difference.

 

 

Without wrapping it in a function it works, but when I do it breaks the function.

 

It may help if we see more code. Of course, you'll want to remove an sensitive information such as the database username and password.

 

 

Have you tried passing the database object as a function argument?

<?php
function grab_function($functionName, $language, $conn){
    $sql = "SELECT * FROM  `functions`
            WHERE  `language` =  '$language'
            AND  `name` =  '$functionName'
            LIMIT 1";
    //... 
}
?>

Also, do you have all PHP errors and warnings being displayed? To make sure, you can add the following to the top of your script during the debugging process:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
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.