结果集为空时显示一条消息 [英] Displaying a message when resultset is empty

查看:85
本文介绍了结果集为空时显示一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,如果结果集为空,则代码将继续处理结果。我要显示的只是查询失败。

In the following code, if the resultset is empty, the code continues to process the result. What I want instead is to just display "Query failed." when there are no results.

$connInfo = array('UID'=>$user, 'PWD'=>$passwd, 'Database'=>$database);
$dbconn = sqlsrv_connect($server, $connInfo);

if($dbconn === false){
    die("<br />Error connecting to the database.<br />");
}
//SQL Query
$query = "SELECT ... FROM somehwere";

//Run Query
$qresult = sqlsrv_query($dbconn, $query);
if($qresult === false) {
    die('Query failed.');
}

?>
...more code...


推荐答案

$ qresult如果未找到任何行,则将包含一个空结果集,但仍不会得出false。

$qresult will contain an empty result set if no rows are found, but it still won't evaluate to false.

请尝试以下函数:

http:// www .php.net / manual / en / function.sqlsrv-num-rows.php

因此:

if(!sqlsrv_num_rows($qresult)) {
    die('Query failed.');
}

而不是:

if($qresult === false) {
    die('Query failed.');
}

这篇关于结果集为空时显示一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆