2016-03-05 38 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 ... 
}