PHP get_result() 返回一个空的结果集


php get_result() returns an empty result set

所以,我有一个准备好的语句,我已经成功地准备、绑定和执行了:

SELECT * FROM users WHERE email = ? AND pass = SHA1(?)

看起来像预期的那样返回了一行。但是,为什么当我调用@ $stmt->get_result()时$result变量为空?提前谢谢。

$num_rows = mysqli_num_rows($stmt->get_result());
if ($num_rows == 1) {
    // Fetch the result set
    $result = $stmt->get_result();
    //if result empty echo false
    if(empty($result)) {
        echo "result is empty";
    }   
}

只是为了把两个评论放在一起,并详细说明一个点......

<?php
$result = $stmt->get_result();
$num_rows = mysqli_num_rows($result);
if ($num_rows == 1) {
    // already fetched the mysqli_result
    // now lets fetch the one record from that result set
    $row = $result->fetch_assoc();
    // ....do something with row
}
else {
    // either 0   ...or more than 1 row
    foo();
}

但是你甚至可以摆脱对 mysqli_num_rows() 的调用(所以它在未缓冲查询的情况下也有效)

$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ( !$row ) {
    // no such record
    foo();
}
else {
    // ....do something with row
    // might want to check whether there are more matching records
    // given the context there shouldn't, but ...
}