2016-03-05 84 views
0

所以,我有一份声明,我已经成功地制备,约束,并执行:PHP get_result()返回一个空的结果集

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"; 
    } 
} 
+2

愚蠢的问题,但你调用'get_result()'在你的代码的开始,然后再调用它里面的if语句 - 你确定它会回到你期待什么第二次叫它? – andrewsi

+1

尝试'fetch_array()' – devpro

回答

1

只是把两点意见一起,制定一个豆蔻....

<?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()(所以它也可以在的情况下, unbuffered queries

$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 ... 
}