1

好的,就这样吧。

我正在尝试编写一个博客系统(无论如何都要学习如何),我遇到的问题是 SQL 只返回数据库中的第一行。它做的次数和我的记录一样多。我无法弄清楚出了什么问题。有人可以指出我正确的方向吗?

<?php 
    include "db_connect.php";
    include "functions.php";
    include "style/header.php";
?>

<link href="style/style.css" rel="stylesheet" type="text/css">

<div id="main">

<?php
       echo 'Welcome to the forest, '.$_SESSION['username'];

   $sql = mysql_query("SELECT post_id, post_user, post_title, post_description, post_info, post_date FROM posts");
   $row = mysql_fetch_array($sql);

   $post_id = $row['post_id'];
   $post_user = $row['post_user'];
   $post_title = $row['post_title'];
   $post_description = $row['post_description'];
   $post_info = $row['post_info'];
   $post_date = $row['post_date'];
   do { ?>

<article>
<h2>

<?php 
echo  $post_title; 
?>
</h2>
<?php
    echo $post_description;
    echo $post_info;
    echo 'Posted by '.$post_user.' on '.$post_date;

} while ($row = mysql_fetch_array($sql)); 
?>
</article>
</div>
4

1 回答 1

1

你最好这样写你的代码(例如,从这个页面

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

另请注意,不推荐使用 mysql_* 函数,因为这些函数已被弃用,并将在 PHP 的未来版本中删除。

于 2013-08-10T02:59:44.513 回答