Problem in PHP MYSql Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Problem in PHP MYSql Query

Post by nithinkk »

i got the following MYSQL query...im very new to PHP

select count(online) from login_details where online='y';

this will result the total count of online users ....eg;10

How can i store it in PHP variable or print this value ????
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem in PHP MYSql Query

Post by Christopher »

I would do something like:

select count(online) AS online_users from login_details where online='y';

The code is something like:

Code: Select all

$result = $db->query("select count(online) AS online_users from login_details where online='y'");
$row = $result->fetch();
echo $row['online_users'];
(#10850)
lathifmca
Forum Newbie
Posts: 6
Joined: Wed Jan 27, 2010 10:56 am

Re: Problem in PHP MYSql Query

Post by lathifmca »

hi nithinkk,
i m a newbie around here, just try this piece of code.. :)

Code: Select all

<?php
    $con = mysql_connect("localhost","root","") or die('Error Connect'); // Connecting to MySQL
    mysql_select_db("test",$con) or die('Error in DB'); // Selecting DB
    $res = mysql_query(" Select count(online) as cnt from login_details where online='y' ",$con); /* Fetching the count value*/
    $row=mysql_fetch_array($res); // fetching the resulted row as array
    echo $row['cnt'];          // displaying the Count value
    mysql_close($con);  // Closing Connection
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Problem in PHP MYSql Query

Post by onion2k »

That's a really horrible way of counting users online. Just set a 'last log in' timestamp when they log in, and count all the users where the timestamp is newer than 5 minutes ago. That way you don't need to bother setting the "online" flag back to 'n'.
Post Reply