PDO mysql query problem
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...
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.
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>';
}
精彩评论