douxing6532 2012-08-02 20:29
浏览 33
已采纳

PHP只返回第一个MySQL结果

I have a loop that should display a summary of all MySQL results. It does this by determining how may results there will be and then loops that many times. So if there are 43 results that match my query, instead of displaying all 43 results, it only displays the first result 43 times. Please help! Here's my code:

if (empty($res)) {

echo 'No results found';

} else if($num>1){

    echo "<b>".$num_rows."<b> results found...<br>";

    while ($res = mysql_fetch_assoc($result)) {

    echo "<a href='#'>{$res['dealer']}</a><br>";

    }

} else {

echo "<table border=\"0\"><tr><td colspan=\"2\"><span class=\"dealer\">" . $res['dealer'] . "</span></td></tr><tr><td><span class=\"label\">Pin: </span><span class=\"inf\">" . $res['pin'] . "</span></td><td><span class=\"label\">OB CODE: </span><span class=\"ob\">" . $res['ob'] . "</span></td></tr><tr><td><span class=\"label\">Program Director:</span></td><td><span class=\"inf\">" . $res['contact'] . "</span></td></tr><tr><td valign=\"top\"><span class=\"label\">Address:</span></td><td><span class=\"inf\">" . $res['address'] . "<br>" . $res['city'] . ", " . $res['state'] . "<br>" . $res['zip'] . "</span></td></tr><tr><td><span class=\"label\">Dealer Phone:</span></td><td><span class=\"inf\">" . $res['phone'] . "</span></td></tr><tr><td><span class=\"label\">Codes Valid on:</span></td><td><span class=\"inf\">" . $res['website'] . "</span></td></tr></table>";

}

Thanks a lot in a advance!

  • 写回答

4条回答 默认 最新

  • dtpk04526211 2012-08-02 20:33
    关注

    That's because you've only fetched the first row (when you did $res = mysql_fetch_assoc($result);). What you're trying to do is actually unnecessary: mysql_fetch_assoc() will automatically move the data pointer ahead every time it runs. What you can do instead (and is in fact common practice) is this:

    //...
    while ($res = mysql_fetch_assoc($result)) {
        echo "<a href='#'>{$res['dealer']}</a><br />";
    }
    //...
    

    On a side note, the mysql_* functions are soon to be deprecated. You can use either mysqli or PDO instead; if you're a lazy bum with a lot of code to switch (like me), you can use the mysqli procedural functions - they're almost identical to the original mysql_* functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题