Jump to content

Call to a member function query() on a non-object


peter_anderson

Recommended Posts

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

Link to comment
Share on other sites

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;";

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.