douketangyouzh5219 2013-08-10 02:55
浏览 52
已采纳

坚持SQL只重复第一行

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?

<?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条回答 默认 最新

  • dqys98341 2013-08-10 02:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?