duanne9313 2018-06-12 05:20
浏览 49
已采纳

尝试回显注释时显示错误[重复]

I am trying to create a comment section on my website. However, my comments aren't showing because of this error

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\tutorials\comments.inc.php:24 Stack trace: #0 C:\xampp\htdocs\tutorials\homepage.php(23): getComments(Object(mysqli)) #1 {main} thrown in C:\xampp\htdocs\tutorials\comments.inc.php on line 24

Here is the code to comments.inc.php

<?php
include 'dbh.inc.php';

function setComments($conn) {
    if(isset($_POST['commentSubmit'])){
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $message = $_POST['message'];

        $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', 
'$date', 
'$message')";
        $result = $conn->query($sql);

   }


}

function getComments($conn) {

    $sql = "SELECT * FROM comments";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()){
    echo "<div class='comment-box'><p>";
        echo $row['uid']."<br>";
        echo $row['date']."<br>";
        echo $row['message'];
     echo "</p></div>";

    }
}
?>

If someone could provide me with an answer that would be great and thank you in advance.

</div>
  • 写回答

1条回答 默认 最新

  • duanfen2008 2018-06-12 05:28
    关注

    The query method can return false instead of a result set in case there is an error. That is why you get the error on the fetch_assoc method call, which obviously does not exist when $result is false.

    This means you have an error in your SELECT statement. To get that error displayed, do this:

     $result = $conn->query($sql) or die($conn->error);
    

    Most probably you have a wrong spelling for the table name or a column name. Maybe when moving to the host you did not create that table correctly and made a spelling mistake there.

    You could also add this for debugging:

    echo "total rows: " . $result->num_rows;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加