2014-10-06 60 views
0

我试图做一个消息,如果没有结果从mysqli的结果显示:如果get_result或fetch_array空不工作

$result = $stmt->get_result(); 

// I tried many things like this before the while 

if (empty($stmt->get_result())) { 
    // message here 
} 

// or 

if (!$stmt->get_result()) { 
    // message here 
} 

while ($row = $result->fetch_array()) { 
    // do something 
} 

//Also after the while 

if (empty($result->fetch_array())) { 
    // message here 
} 

// or 

if (!$result->fetch_array()) { 
    // message here 
} 

但没有什么工作,我得到的,而打印数据和消息也显示...或不显示任何内容。

什么是正确的方法?

谢谢!

+0

我经常使用['rowCount()'](http://php.net/manual/en/pdostatement.rowcount.php)。在[examples](http://php.net/manual/en/pdostatement.rowcount.php#refsect1-pdostatement.rowcount-examples)和[user notes](http:// php .net/manual/en/pdostatement.rowcount.php#usernotes)。 – showdev 2014-10-06 23:32:26

回答

0

mysqli_stmt::get_result()只会在错误时返回false,所以查找负数不是检查空结果集的方法。您可以使用示例代码中的while循环来增加计数器并在事实后检查其值。或者您可以使用返回的mysqli_result对象并检查其mysqli_result::$num_rows属性。

尽管如果数据库端存在错误,您应该继续检查false作为返回值。这些可以用mysqli::$error显示。

+0

你能举个例子吗? – 2014-10-07 00:07:29