Q695 Posted March 4, 2015 Share Posted March 4, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/ Share on other sites More sharing options...
cyberRobot Posted March 4, 2015 Share Posted March 4, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507539 Share on other sites More sharing options...
Q695 Posted March 4, 2015 Author Share Posted March 4, 2015 (edited) 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 March 4, 2015 by Q695 Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507541 Share on other sites More sharing options...
cyberRobot Posted March 4, 2015 Share Posted March 4, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507544 Share on other sites More sharing options...
Q695 Posted March 4, 2015 Author Share Posted March 4, 2015 I tried it outside of the function, which worked, now I'm building the function finder function around it. Should I switch to mysqli to try to solve it that way? Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507548 Share on other sites More sharing options...
Q695 Posted March 5, 2015 Author Share Posted March 5, 2015 Without wrapping it in a function it works, but when I do it breaks the function. Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507578 Share on other sites More sharing options...
fastsol Posted March 5, 2015 Share Posted March 5, 2015 Show us the code that is setting $conn outside the function. Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507584 Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/295103-why-cant-i-run-q-conn-querysql-in-a-function/#findComment-1507621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.