0

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.

2
  • mysql_query and its related family of functions are ancient and you should no longer be using them. They have been deprecated, and will shortly be removed from the language. Please find a tutorial on PHP Data Objects. Commented Oct 6, 2013 at 19:59
  • @meagar Yes, I am aware of that. That's not my problem here, though. Commented Oct 6, 2013 at 20:02

1 Answer 1

1

You should loop it like (mysql_fetch_assoc)

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

Also, notice the Warning.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, I am aware of the warning. I'm currently working on going through my site and updating all of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.