This question already has an answer here:
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>