I’ve never used the new mysqli class in php as the old mysql interface was always good enough however I’m trying to update some old code to use mysqli and things are not going so well and I’m getting the following error:
[error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_row() on a non-object in [location of file]
However I know the query is good because I can echo it and use it directly on the db to give me good results. I have a feeling I’m using the “fetch_row() incorrectly.
Can someone suggest what I’m doing wrong?
/// class setup stuff
public function query($query,$sanitize=TRUE)
{
if($sanitize!==FALSE)
{
$query=$this->mysqli->escape_string($query);
}
echo $query;
$this->mysqli->query($query);
echo $this->mysqli->error;
//$this->mysqli->free();
}
public function select($table,$what,$where,$orderby=FALSE,$order=FALSE,$limits=FALSE,$sanitize=TRUE) //this is really simple and very limited
{
//process the select query and send to query method
$query = "SELECT $what FROM $table WHERE ";
$i = 0;
foreach($where as $key => $value)
{
$key = $sanitize?$this->mysqli->escape_string($key):$key;
$value = $sanitize?$this->mysqli->escape_string($value):$value;
$query .= "$key='$value' ";
$i++;
if($i<count($where))
{
$query .= "AND ";
}
}
// check the max rowcount
$this->query($query,FALSE);
$result = $this->mysqli->use_result(); //<--this is the issue
Thanks to @scones’ suggestion I tested the connection after initializing it and found that the issues was the ‘use_result’ and the fact that I hadnt returned the result in the query function. Here’s the working version notice
$result = $this->query($query,FALSE);