【问题标题】:updating while loop to mysqli将while循环更新为mysqli
【发布时间】:2012-09-09 10:59:31
【问题描述】:

大家好,我有以下问题要解决。我有一个自动建议搜索字段。我想将我的代码从使用 mysql 更新为 mysqli。这就是为什么我有这个代码($db=mysqli):

if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {

    $search_term = $db->real_escape_string(htmlentities(trim($_POST['search_term'])));

    $search_term_query = "SELECT `a` FROM `b` LIKE '$search_term%'";
    $result_search_query = $db->query($search_term_query);

    while (($row = $result_search_query->fetch_assoc()) !== false) {

        echo '<li>', $row['a'], '</li>';
    }

}

由于某种原因我得到:

在非对象上调用成员函数 fetch_assoc()

那么对象的问题在哪里?如果有人可以帮助我,我真的很感激。非常感谢。

【问题讨论】:

  • 请说明$db是如何定义的
  • $db = mysqli 来自另一个文件。

标签: php mysql html loops mysqli


【解决方案1】:

这意味着 $result_search_query 不是您期望的对象。可能的原因:

  1. 与数据库的连接失败
  2. 查询执行失败

请查看http://php.net/manual/en/mysqli-result.fetch-assoc.phphttp://www.php.net/manual/en/mysqli.query.php 中的示例。它应该向您展示如何捕获错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2018-08-13
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多