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.

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

报告相同问题?