[英]Mysqli query returns no rows
無論出於何種原因,我不斷得到echo "No News.";
的echo "No News.";
即使我清楚地將信息放入表中,也就是名為news
。
<?php
session_start();
define('DB_SERVER', "localhost");
define('DB_USER', "USERNAME");
define('DB_PASSWORD', "PASSWORD");
define('DB_DATABASE', "DATABASE");
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// Check connection
if(mysqli_connect_errno()) {
echo "Email Owner@OtherTXT.com";
exit();
}
/* create a prepared statement */
$query = "SELECT `title`, `message`, `date` FROM `news`";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>";
echo ($row["title"]);
echo "</h2>";
echo "<h3>";
echo ($row["date"]);
echo "</h3>";
echo "<br />";
echo "<p>";
echo ($row["message"]);
echo "</p>";
}
} else {
echo "No News.";
}
$mysqli->close();
?>
這是我桌子的照片
希望這會有所幫助;)
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>";
echo ($row["title"]);
echo "</h2>";
echo "<h3>";
echo ($row["date"]);
echo "</h3>";
echo "<br />";
echo "<p>";
echo ($row["message"]);
echo "</p>";
}
} else {
echo "No News.";
}
也許在調用num行之前添加store_result可以解決問題:
$result = $mysqli->query($query);
$result->store_result();
if ($result->num_rows > 0) {
...
或者您可以將其放入查詢中:
$result = $mysqli->query($query,MYSQLI_STORE_RESULT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.