peter_anderson Posted September 24, 2011 Share Posted September 24, 2011 I have a database called "mp_users" with the following setup: CREATE TABLE IF NOT EXISTS `mp_users` ( `id` int(10) unsigned default NULL auto_increment, `uid` int(10) unsigned NOT NULL, `username` varchar(100) NOT NULL, `email` varchar(50) NOT NULL, `points` int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; When I try to run the following query, I get an error: SELECT COUNT(id) AS user_exists FROM `mp_users` WHERE uid=$uid LIMIT 1; Error: Fatal error: Call to a member function query() on a non-object in /home/user/public_html/marketplace-functions.php on line 52 Here is the code: <?php require_once "config.php"; $sql = new mysqli($db1['host'], $db1['user'], $db1['pass'], $db1['db']); function checkUserExists($uid){ // Query $q = "SELECT COUNT(id) AS user_exists FROM `mp_users` WHERE uid=$uid LIMIT 1;"; echo $q; $q = $sql->query($q); $r = $q->fetch_assoc(); // Any results? if($r['user_exists'] == 0){ // redirect to runonce... header('Location: marketplace.php?do=runonce&return='.urlencode($_SERVER['QUERY_STRING'])); exit(); } } ?> Line 52 is the $q query. I really cannot see what the problem is. The database exists, I've tried it in phpMyAdmin and it runs without trouble, but this wont! Can anyone help? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/247798-call-to-a-member-function-query-on-a-non-object/ Share on other sites More sharing options...
Nethox Posted September 24, 2011 Share Posted September 24, 2011 did you make your query method static? if so I think you need to call it like this sql::query() instead (assuming it IS part of a class) EDIT : Also, I think you should change your query to : $q = "SELECT COUNT(id) AS user_exists FROM `mp_users` WHERE uid=".$uid." LIMIT 1;"; Quote Link to comment https://forums.phpfreaks.com/topic/247798-call-to-a-member-function-query-on-a-non-object/#findComment-1272479 Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 I think this is a scope issue. As $sql is not available in the function. Try passing the Database Object to the function. Quote Link to comment https://forums.phpfreaks.com/topic/247798-call-to-a-member-function-query-on-a-non-object/#findComment-1272496 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.