1

I am building a login/signup site with PDO just for fun on wamp...

I am currently building a users online page, when a user accesses any member page it updates a column called online in my mysql table with a unix timestamp.

On the users online page I am trying to find and show all the users who qualify for being online which is being active in the last ten minutes. So far I have this...

$online_users = time() - 600; $q = $dbc -> prepare("SELECT * FROM accounts WHERE online > ?"); $q -> execute(array($online_users)); $online_user = $q -> fetch(PDO::FETCH_ASSOC); while ($q) { echo '<p> ' . $player_details['username'] . ' is online.</p>'; } 

The problem I am having is that it is looping over and over again, I would like it to loop through the table once, and display all the users who the mysql query only once...

I am new to PDO and am self learning, so if it is something really daft please excuse me!

Thanks in advance...

3
  • BTW, aren't you getting a bunch of notice's? Commented Aug 25, 2011 at 8:03
  • No I just got an infinite page of the results. Commented Aug 25, 2011 at 8:06
  • You are storing info in $online_user and reading it from $player_details. PHP can warn you about it and help you save time and headaches: have a look at error_reporting(). Commented Aug 25, 2011 at 8:36

2 Answers 2

2

Don't read once first and loop forever afterwards:

$online_user = $q -> fetch(PDO::FETCH_ASSOC); while ($q) { 

Instead, read on every loop iteration:

while ($online_user = $q -> fetch(PDO::FETCH_ASSOC)) { 

Additionally, don't forget to htmlspecialchars() accordingly.

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

2 Comments

htmlspecialchars?? I thought PDO checked all this for me?? could you elaborate please, and thanks for your quick and helpful answer will accept as soon as I can :)
PDO has no clue about what your data contains and what you are going to use it for. When a programming language makes assumptions about it, you end up with terrible mistakes like magic quotes.
0

Also you use the following code:

$online_users = $q->fetchAll(PDO::FETCH_ASSOC); foreach($online_users as $user) { echo '<p> ' . $user['username'] . ' is online.</p>'; } 

3 Comments

is there any difference to using the your answer or the one above yours??
Principally no. You can try to check speed of each answer and select the best.
@carlgcoder: This stores the complete result set into a PHP variable. If you only need to use your data once, as in your question, it doesn't really matter... unless you are retrieving 10,000 rows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.