cosmicsafari cosmicsafari - 2 months ago 5
PHP Question

Fatal error: Call to a member function close() on a non-object. MySQLi issue

this is my first post so go easy on me ^_^

I have been getting the following error when i uploaded to a live server. It works ok on localhost which i thought was strange. Any help would be greatly appreciated.

Fatal error: Call to a member function close() on a non-object....


The line it refers to

$stmt->close();


The connection to the DB

$connection=new mysqli($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS,$DB)or die(mysqli_error($connection));


The class itself.

function getTimes(){ //this method just pulls the results of the query and returns them as an array
global $connection;
$route = $this->route;
$station = $this->station;
$day = $this->day;

// create a prepared statement
if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) {
$stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers
$stmt->execute(); //execute query
$stmt->bind_result($col1); //bind result variables
while ($stmt->fetch()){
$results[]=$col1;
}
}
$stmt->close();//close statement
return $results;
}

Answer

You should put $stmt into you if clause. There is a possiblity that if (false) and still get to your $stmt->close();