如何在PHP中在线显示用户?

时间:2016-06-30 19:01:10

标签: php mysql

此代码仅显示“1个用户在线”,无论有多少人在线。我该如何解决这个问题?

<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$online = "1";
$query = "SELECT * FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_array($data);
$online=$row['online'];
echo '<div id="online-me" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>

2 个答案:

答案 0 :(得分:1)

所以用

Content-Language

您正在获取$query = "SELECT * FROM `users` WHERE online = '$online'"; 所在的行。然后设置online = 1,无论如何都将$online=$row['online'];设置为$online

这应该会让你知道如何解决它。 干杯!祝你好运!

答案 1 :(得分:1)

使用MySql的count() 也使用mysqli_fetch_assoc而不是mysqli_fetch_array

类似这样的事情

<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$online = "1";
$query = "SELECT count(id) as 'total' FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($data);
$online=$row['total'];
echo '<div id="online-me" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>