好的,所以这里。
我正在尝试编写一个博客系统(无论如何都要学习),而我遇到的问题是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>
答案 0 :(得分:1)
您可以更好地编写这样的代码(例如,来自this page)
$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版本中删除。