Fatal error: Call to a member function close() on

2020-01-27 08:10发布

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;
    }

标签: php class mysqli
3条回答
Explosion°爆炸
2楼-- · 2020-01-27 08:46

Move the close() call into your if statement, so that it will only be called if a $stmt was successfully created.

    // 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
    }

I suspect on your local machine you don't have errors turned on, but on the server you upload to, errors are on.

The root issue to address here is the fact that prepare() is failing. The problem there most likely is the database on the server is missing the timetable table or that table is missing one or more fields route, day or station. As said by cbuckley, check $connection->error for the full error message.

When you uploaded to the server, did you remember to also make any database structure changes on the server?

查看更多
太酷不给撩
3楼-- · 2020-01-27 08:47

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

查看更多
The star\"
4楼-- · 2020-01-27 08:52

Your problem was that the $stmt object was instantiated as part of an if condition test. In the cases it failed, i.e. when it returns false, you were still trying to call ->close() on it anyway. I moved the method call within the if block.

Now you need to add an else clause to handle the fact that your script couldn't prepare the statement and given you say this works locally but not on your live server, I suggest there is some configuration difference causing a problem here. You need to turn on error handling with display_errors('1') and error_reporting(E_ALL). Don't forget to turn these off before letting the world at your new script. :)

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;
    }
查看更多
登录 后发表回答