Page 1 of 1

Problem in PHP MYSql Query

Posted: Thu Jan 28, 2010 12:15 am
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 ????

Re: Problem in PHP MYSql Query

Posted: Thu Jan 28, 2010 2:23 am
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'];

Re: Problem in PHP MYSql Query

Posted: Thu Jan 28, 2010 2:37 am
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
?>

Re: Problem in PHP MYSql Query

Posted: Thu Jan 28, 2010 2:45 am
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'.