致命错误:在非对象上调用成员函数 close()。MySQLi 问题

cos*_*ari 5 php mysqli class

上传到实时服务器时出现以下错误。它在本地主机上运行正常,我认为这很奇怪。

致命错误:在非对象上调用成员函数 close()....

它所指的行

$stmt->close();
Run Code Online (Sandbox Code Playgroud)

与数据库的连接

$connection=new mysqli($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS,$DB)or die(mysqli_error($connection));
Run Code Online (Sandbox Code Playgroud)

班级本身。

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;
}
Run Code Online (Sandbox Code Playgroud)

Chu*_*urk 4

你应该把$stmtif 子句放进去。有可能 if (false) 仍然到达你的$stmt->close();