douxing7101 2017-05-13 19:26
浏览 27
已采纳

数据库返回Bool而不是SQL

I'm having a problem when I try to run a search program I made. When I do:

if (isset($query)) {
    $query = preg_replace("#[^0-9a-z]#i", "", $query);
    $query = array($db->q("select * from posts where title like $query") , 
    $db->q("select count(*) from posts where title like $query"));  

    if ($query[1] == 0){
        $out = "Sorry: There is no results";
    } else {
        while ($row = mysql_fetch_array($query[0])){
             $title = $row['title'];
             $exep = $row['exepert'];
             $out .= "<div>".$title."".$exep."</div>";
        }
    }
}

It always says there are no search results, but when I force it to go to the else statement it says both the results and the count are booleans (the number 0).

If anybody needs extra code to help me please ask!

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doujing5150 2017-05-13 19:52
    关注

    Assuming that your magical $db object there is some kind of wrapper for MySQLi, both MySQLi::query() and MySQLi::prepare() will return false on failure (see the dock) by default.

    You should either alter your code to deal with it directly or right after establishing of connection do this:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    It will cause all SQL errors to throw exception. IMHO, that makes for easier development process. And you can also integrate those exceptions in your persistence logic, since they will also get thrown in case of integrity violations (like, when user attempts to make a duplicate in a column marked as UNIQUE).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制