douyan4900 2017-07-24 09:07
浏览 453

$ conn-> query(...)的结果可能不为null且有0条记录

I came across the following in some old PHP code that I have to work on. My question is, are both those Ifs required? In other words, if a result is returned it must have returned greater than zero records, right? And the converse - meaning if $result is False, can I assume that no records have been found?

$sql = "SELECT * FROM houses WHERE ownerphone=$pn";
$result = $conn->query($sql);
if ($result) {
    $count = $result->num_rows;
    if ($count > 0){
       ....Stuff happens here...
    } else {
        ...What happens here?...
    }
} else {
     ...Other Stuff happens here
}
  • 写回答

3条回答 默认 最新

  • dongli7870 2017-07-24 09:09
    关注

    Yes, both are required.

    if a result is returned it must have returned greater than zero records, right?

    Wrong.

    If you don't get a result then you got an error and you won't have an object to read the number of results back from.

    You can get zero rows back from a successful request.

    if $result is False, can I assume that no records have been found?

    Well, yes. Sort of. That's why the if statement stops you checking to see if there are a number of rows if the result is False.

    … but not getting rows back because there was an error is different to not getting rows back because there were none to find.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?