Jump to content

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


peter_anderson

Recommended Posts

I am changing my script from connecting to the database within every function, to using one connection (in the main class).

 

However, I am getting an error:

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

 

Here is  the main class:

<?php
class main{
// the configuration vars
public $config;
// the current user variables
public $uid = 0; // contains the user ID
public $gid = 0; // contains the group ID
// database variables
public $DB; // contains the connection
public $query_id; // contains the query ID
public $query_count; // how many queries have there been?
// cache variables
public $cache;
public $test = 'hi';
// initiate
public function __construct(){
}
// start up functions
public function startup(){
	// first, set up the caching
	if($this->config['use_memcache'] == true){
		// start up cache
		require_once('cache.class.php');
		$this->cache = new cache_TS();
	}
	// now, set up the DB
	$this->connectToDatabase();
	// now, set up the user session
	$this->setUserSession();
	// are we logged in? If so, update our location
	if($this->uid > 0){
		// update location
		$this->updateUserSession();
	}
}
// connect to database
public function connectToDatabase(){
	// connect to database
	$this->DB = new mysqli($this->config['host'],$this->config['user'],$this->config['pass'],$this->config['db']);
	// were we able to connect?
	if(!isset($this->DB)){
		// this is an error
		exit('could not connect to the database.');
	}
	// test query
	$q = "SELECT id,title FROM topics WHERE id='1' LIMIT 1;";
	$q = $this->DB->query($q);
	$r = $q->fetch_assoc();
	echo $r['id'] . $r['title'];
	// send back
	return $this->DB;
}
// some removed....

 

The test query works fine.

 

It fails here:

<?php
class forum extends main{
public function viewTopic($tid){
	// Connect To Database
	//$sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);		
	// Get assorted details (topic, forum, etc)
	// Get topic details
	$tid = intval($tid);
	$tq = "SELECT * FROM `topics` WHERE id='$tid' LIMIT 1";
	$tq = $this->DB->query($tq);
	$tr = $tq->fetch_assoc();

 

startup in main has already been called before forum is. The DB connects without failure.

 

Can anyone see what the problem is? How can I get $this->DB->query to work? (The query works fine when it's ran in the main class)

Link to comment
Share on other sites

I'm not going to comment on the actual issue here as the actual design here is completely floored.

 

forum appears to extend main simply because it needs a database connection. This is not the correct way to use inheritance. Object should only extend another object if they are of the same type. eg; dog extends animal.

 

To gain access to a database connection within a class you need to pass the connection in as a dependency. This can be done in a number of ways, the most common is to pass it in through the construct. eg;

 

$db = new MySql;
$form = new forum($db);

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.