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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.