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!