致命错误:在第17行的......中的非对象上调用成员函数fetch_assoc()[重复]
问题描述:
This question already has an answer here:
After migrating from localhost to webserver I'm getting this error. Maybe it could be because of the PHP version on webserver (PHP 5.2). Any ideas?
<?php
$servername ="";
$username ="uran";
$password ="";
$dbname ="";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM answer WHERE topic_key='$id'";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>Príspevok č.:</strong> " . $row["id"]. "<br>"." <strong>Napísal:</strong> "
. $row["name"]. "<br>". $row["topic"]."<br>" . $row["reg_date"]."<br>"." <br><br>";
}
$conn->close();
?>
</div>
答
I think that your query fails to that $conn->query($sql)
will return FALSE
(see mysqli::query).
Please add the following code to your code to get the error message:
if (!$result) {
printf("Error: %s
", $conn->error);
}