从数据库检索两个记录

时间:2018-12-03 02:22:55

标签: php mysqli

我正在学习php和ajax,今天却被卡住了,我的代码如下:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ajax";

$conn = mysqli_connect($servername, $username, $password, $dbname);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="jquery.min.js"></script>
</head>
<body>
<div id="comments">
    <?php 
        $sql = "SELECT * FROM comments LIMIT 2";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result)> 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                echo "<p>";
                echo $row['author'];
                echo "<br>";
                echo $row['message'];
                echo "</p>";
            }
        } else {
            echo "There are no comments!";
        }
    ?>
</div>
</body>
</html>

我想做的是检索两个记录,并回显每个记录的作者和消息,语法似乎正确,但是浏览器向我输出了以下纯文本格式:

0) { while ($row = mysqli_fetch_assoc($result)) { echo "

"; echo $row['author']; echo "
"; echo $row['message']; echo "
"; } } else { echo "There are no comments!"; } ?> 

这似乎是对代码或其他内容的误读,但是我找不到问题。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题