Error when using an Object oriented function and mysqli

Hello, I’m new here and new to mysqli. I’m making a cms forum that was originally using MySQL but I’m switching to Mysqli, and when I try I’m getting an error:

Fatal error: Call to a member function fetch_array() on a non-object in /home/u250000297/public_html/forum/system/db.php on line 50

I have my connection and query/fetch function in my db.php file, and I’m using my fetch function in header.php to get the user account info. I’ve tried multiple things, but nothing seems to work and I’m getting pretty frustrated. Any ideas on what to do and what the problem is?

db.php:

<?php


class db {

    var $host = '';
    var $username = '';
    var $password = '';
    var $dbmaster = '';
    var $dbslave = '';
    var $db = '';

    function connect($host, $username, $password, $database) {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
		
		// 1. Create a database connection
		$mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
		//2. check if the database is connected
		if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $mysqli->connect_error . ']');
}
    }



	//Execute the query
    function query($mysqli, $query) {
       global $mysqli;
       return $mysqli->query($query);
    }
	
	//Get array of query results/ this is the problem code.
    function fetch($mysqli, $query) {
       global $mysqli;
	return $query->fetch_array(MYSQLI_ASSOC); //Problem line
}
	
	
	//Close the connection
    function close() {
        mysqli_close($this->database);
    }

}

?>

My some of my header.php :

// Check if the info is good and ready.
include('./system/config.php');

//Require the db file.
include('./system/db.php');

// Load the seleceted database.
$db = new db();
$db->connect($dbhost, $dbuser, $dbpassword, $dbmaster);

// Check if the user is logged in by their cookies.
if(isset($_COOKIE["email"]) && isset($_COOKIE["lpip"]) ) {
	$email = $_COOKIE["email"];
	$lpip = $_COOKIE['lpip'];
} else {
	$email = '';
	$lpip = '';
	//If there are no cookies, clear the past cookies if logged on on different device.
	 $year = time()-3600;
	 setcookie('email', $_SESSION['email'], $year, '/', '.imperiousart.tk');
	 setcookie('lpip', $_SESSION['lpip'], $year, '/', '.imperiousart.tk');
}

//Get the account iformation of the user(s) or guest.
$account = $db->fetch($mysqli, "SELECT * FROM accounts WHERE email = '$email' AND lpip = '$lpip'");

I’ve tried google and nothing seems to work for me. Help please!

http://us2.php.net/mysqli_fetch_array

The full function name is mysqli_fetch_array()

Still the error?

What do you mean by object-oriented function? As far as I know, a function in PHP cannot be object oriented since functions are not first class objects in this language.

Hi Xzbit3, welcome to the forums!l

The problem here is that you’re calling the fetch method, trying to pass in the $mysqli variable, and a string containing your SQL:

$account = $db->fetch($mysqli, "SELECT * FROM accounts WHERE email = '$email' AND lpip = '$lpip'");

but inside the fetch method, you’re trying to treat the second argument as a query object, calling the fetch_array() method… but as $query is actually just a string, it has no methods, which is why you’re getting that error.

Also, as your DB class is supposed to be a wrapper around a msqli connection object, you don’t need to pass the connection into all the methods. Take a look at these two methods:


//Execute the query 
function query($mysqli, $query) { 
   global $mysqli; 
   return $mysqli->query($query); 
} 
 
//Get array of query results/ this is the problem code. 
function fetch($mysqli, $query) { 
	global $mysqli; 
	return $query->fetch_array(MYSQLI_ASSOC); //Problem line 
} 

you’re (presumably) passing in a mysqli connection object as $mysqli, and you’re also trying to pull it in from a global variable.

What you could do is change your DB class a little:


class db { 
 
    protected $db; 

    public function __construct($host, $username, $password, $database)
    {  
        $this->db = new mysqli($host, $username, $password, $database); 
        
        if ($db->connect_errno) { 
            throw new Exception('Unable to connect to database [' . $mysqli->connect_error . ']');
        } 
    } 

    //Execute a query 
    public function fetch($query)
    { 
        $res = $this->db->query($query);
        return $res->fetch_assoc();
    } 

} 

which you would then use like this:


// Create database connection. 
$db = new db($dbhost, $dbuser, $dbpassword, $dbmaster); 

// Get the account information of the user(s) or guest. 
$account = $db->fetch("SELECT * FROM accounts WHERE email = '$email' AND lpip = '$lpip'");  

This revised class doesn’t need to keep all the DB connection details as parameters, as they’re only needed the once. Once the connection object is created, it’s stored in the $db property, so you can access it from any method by calling $this->db.

It works! Thank your for your help, and identifying my problem. I appreciate all the people the took their time to post and help. Thank you all! :slight_smile: