dqxboe2628 2013-10-06 19:57
浏览 47
已采纳

回显数组值的问题

I am having trouble echoing values from an array. I am using a mysql query to create an array, $online, containing the names of currently online users. Here is my code:

<?php
    $goodbye = time() - 300;
    $qry="SELECT UserName FROM Members WHERE Seen >=$goodbye";
    $result=mysql_query($qry);

    if($result) {
        $online = mysql_fetch_assoc($result);
        foreach($online as $u) {
            echo $u;
            echo "<br>";
        }
    } else {
        die("Query Failed");
    }
?>

When viewing this on my web page, only the first index of the array is shown (as in: if User1, User17, and User69 are all online, only User1 will appear on the list). I'm sure this is happening because I'm using echo incorrectly, but I haven't been able to figure it out yet. Any tips? Thanks.

  • 写回答

1条回答 默认 最新

  • dongxiaofa6359 2013-10-06 20:02
    关注

    You should loop it like (mysql_fetch_assoc)

    if($result) {
        while ($row = mysql_fetch_assoc($result)) {
            echo $row["UserName"];
        }
    }
    

    Also, notice the Warning.

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

报告相同问题?

悬赏问题

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