This script always returns one, not the actual number of online users. Can anyone help fix my code?
$oq = "SELECT user FROM user_archive WHERE time > (NOW() - INTERVAL 5 MINUTE)";
$oresult = mysqli_query($con,$oq);
$online_users = mysqli_num_rows($oresult);
if($online_users = 1)
{
echo "{$online_users} user online";
}
if($online_users != 1)
{
echo "{$online_users} users online";
}
You need to use ==
instead of =
if($online_users == 1)
{
echo "{$online_users} user online";
}
In the line of code where you wrote:
if($online_users = 1)
You are asigning the value 1 to $online_users, instead of comparing the value. It should be:
if($online_users == 1)
Using one equals sign is something called an assignment operator
so:
$Var = 1; // This variable equals to 1
But when validating.
if ($Var == 1){
} // Notice the use of two equals, for a comparison operator
$online_users = 1
must be $online_users == 1
.
And
$oq = "SELECT COUNT(*) FROM user_archive WHERE time > (NOW() - INTERVAL 5 MINUTE)";
or
$oq = "SELECT COUNT(DISTINCT user) FROM user_archive WHERE time > (NOW() - INTERVAL 5 MINUTE)";
COUNT(*)
counts, not what you do.
Why retrieve rows of data
to counts them when you can just get one integer
back and get MySql to count for you?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.