mysqli fetch_assoc() not returning

2019-09-02 18:14发布

I have a webservice which i have developed in PHP using SLIM on my private WAMP server, where everything works fine. When deploying it to my webhotel, msqli fetch_assoc seems to never return and i get a "Response does not contain any data." I confirmed that it never returned by first putting and echo before the fetch_assoc call .. and afterward after the call .. and it never hit my echo when put after fetch_assoc.

My private webserver runs PHP 5.4.12 and MySQL 5.6.12, whereas my webhotel is running PHP 5.3.28 and MYSQL 5.1.71.

Is it my web host that need to upgrade their mysql server, as i have learned that this has been an issue in some mysql server versions ?

In desperate need of help ... Thanks

EDIT 1:

public function getUserByEmail($email) {

    $stmt = $this->conn->prepare("SELECT name, email, api_key, status, created_at FROM user WHERE email = ?");

    $stmt->bind_param("s", $email);
    if ($stmt->execute()) {     

    //$user = $stmt->fetchAll(PDO::FETCH_ASSOC);  echo "afas";  
        $user = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        return $user; 
    } else {
        return NULL;
    }
}

标签: php mysqli
2条回答
祖国的老花朵
2楼-- · 2019-09-02 19:01

Michael Berkowski kindly pointed me in the direction of the answer to my problem.

"get_result() is only available on some systems having the mysqlnd driver."

So i ended up switching to PDO, which did not require a lot of changes to my code..

查看更多
Rolldiameter
3楼-- · 2019-09-02 19:09

Check your phpinfo(). I have mysqlnd, but only the PDO exension. You can tell by looking a the API Extensions under the mysqlnd section (if mysqlnd is installed).

Instead of re-writing everything in PDO, this worked for me:

public function getUserByEmail($email) {
    $stmt = $this->conn->prepare("SELECT name, email, api_key, status, created_at FROM users WHERE email = ?");
    $stmt->bind_param("s", $email);
    if ($stmt->execute()) {
        $stmt->bind_result($name, $email, $api_key, $status, $created_at);

        /* fetch values */
        mysqli_stmt_fetch($stmt);

        /* set values */
        $user['name'] = $name;
        $user['email'] = $email;
        $user['api_key'] = $api_key;
        $user['created_at'] = $created_at;

        $stmt->close();
        return $user;
    } else {
        return NULL;
    }
}
查看更多
登录 后发表回答