简体   繁体   English

Mysqli查询不返回任何行

[英]Mysqli query returns no rows

For whatever reason, I keep getting the echo "No News."; 无论出于何种原因,我不断得到echo "No News.";echo "No News."; even though I clearly have information put into the table which is named 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();
?>

This is a picture of my table 这是我桌子的照片

http://i.imgur.com/7xZTSvd.png

Hope this will help ;) 希望这会有所帮助;)

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.";
}

Maybe adding a store_result before calling the num rows do the trick: 也许在调用num行之前添加store_result可以解决问题:

$result = $mysqli->query($query);

$result->store_result();

if ($result->num_rows > 0) {
...

Or you can put it into the query: 或者您可以将其放入查询中:

$result = $mysqli->query($query,MYSQLI_STORE_RESULT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM