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 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改