坚持SQL只重复第一行

时间:2022-04-08 08:01:02

OK, so here goes.

好的,所以这里。

I'm trying to write a blogging system (learning how to anyway), and the problem I have is that SQL is only returning the first line in the database. It does it as many times as records I have. I can't work out what is going wrong. Can somebody point me in the right direction please?

我正在尝试编写一个博客系统(无论如何都要学习),而我遇到的问题是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>

1 个解决方案

#1


1  

You would be better served to write your code like this (for example, from 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"]);
}

Also please note that using mysql_* functions is not recommended, as these functions are deprecated and will be removed in future versions of PHP.

另请注意,建议不要使用mysql_ *函数,因为这些函数已弃用,将在以后的PHP版本中删除。

#1


1  

You would be better served to write your code like this (for example, from 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"]);
}

Also please note that using mysql_* functions is not recommended, as these functions are deprecated and will be removed in future versions of PHP.

另请注意,建议不要使用mysql_ *函数,因为这些函数已弃用,将在以后的PHP版本中删除。