我试图编写一个系统,显示谁登录到我的网站。
我已经尝试了很多东西,而且我觉得我正在用这个东西 - 但它不起作用。 我需要帮助才能找到我错的地方。
这里是我的显示代码(我知道我不应该使用表格进行格式化,但我正在使用它进行测试):
<? $loggedInUsers2 = "SELECT * FROM techs WHERE Level='0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; $loggedInUsers3 = "SELECT * FROM techs WHERE Level>'0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; ?> <div class="col-sm-4"> <div class="thumbnail"> <center><h4 style="height:35px;">Users Online</h4></center> <div class="modal-body" style="min-height:498px;"> <table> <tr><td> <? mysqli_query($con, $loggedInUsers2) or die("Error " . mysqli_error($con)); while($row2 = mysqli_fetch_array($loggedInUsers2)) { //if level less than 1 echo $row2['Name']."<br/>"; } ?> </td><td> <? mysqli_query($con, $loggedInUsers3) or die("Error " . mysqli_error($con)); while($row3 = mysqli_fetch_array($loggedInUsers3)) { //if level more than 1 echo $row3['Name']."<br/>"; } ?> </td></tr> </table> </div> </div> </div>这是我保存到数据库:
$userId = $_SESSION['UserId']; $loggedInUsers1 = "UPDATE techs SET LastTimeSeen=NOW() WHERE UniqueID='$userId'"; mysqli_query($con, $loggedInUsers1) or die("Error " . mysqli_error($con));这输出到我的数据库(在LastTimeSeen字段)到像2015-12-21 08:35:43 (更新每隔几秒通过jquery重新加载页面的页脚)
基本上,第一页的表格中没有输出。
编辑:
建议使用登录按钮来设置用户活动状态,并使用注销按钮将其设置为非活动状态 - 这是我对此的回应:
"The problem with that is people won't use the logout button. They will just close the browser. I want this to keep track of only users that are online. The footer updates the time in the database, every couple of seconds, and then the table is listed on a part of the page that reloads every few seconds aswell, so they are both always up to date. It should only list people that have been on a page in the past 5 minutes."
I'm trying to write a system that shows who is logged into my website.
I've tried so many things, and I feel like I'm onto something with this - but it isn't working. I need help trying to find where I am going wrong.
Here is my display code (I know I shouldn't format with tables, but I am using it for testing):
<? $loggedInUsers2 = "SELECT * FROM techs WHERE Level='0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; $loggedInUsers3 = "SELECT * FROM techs WHERE Level>'0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; ?> <div class="col-sm-4"> <div class="thumbnail"> <center><h4 style="height:35px;">Users Online</h4></center> <div class="modal-body" style="min-height:498px;"> <table> <tr><td> <? mysqli_query($con, $loggedInUsers2) or die("Error " . mysqli_error($con)); while($row2 = mysqli_fetch_array($loggedInUsers2)) { //if level less than 1 echo $row2['Name']."<br/>"; } ?> </td><td> <? mysqli_query($con, $loggedInUsers3) or die("Error " . mysqli_error($con)); while($row3 = mysqli_fetch_array($loggedInUsers3)) { //if level more than 1 echo $row3['Name']."<br/>"; } ?> </td></tr> </table> </div> </div> </div>Here is my saving to the database:
$userId = $_SESSION['UserId']; $loggedInUsers1 = "UPDATE techs SET LastTimeSeen=NOW() WHERE UniqueID='$userId'"; mysqli_query($con, $loggedInUsers1) or die("Error " . mysqli_error($con));This outputs to my database (In the LastTimeSeen field) to something like 2015-12-21 08:35:43 (Updates every few seconds via jquery reloading the footer of the page)
Basically, there is just no output in the tables from the first page.
EDIT:
It was suggested to use the login button to set a user active, and the logout button to set them inactive - here is my response to that:
"The problem with that is people won't use the logout button. They will just close the browser. I want this to keep track of only users that are online. The footer updates the time in the database, every couple of seconds, and then the table is listed on a part of the page that reloads every few seconds aswell, so they are both always up to date. It should only list people that have been on a page in the past 5 minutes."
最满意答案
当你使用mysqli_query它返回一个结果对象。 你需要将这个对象传递给mysqli_fetch_array而不是查询字符串。
<? $loggedInUsers2 = "SELECT * FROM techs WHERE Level='0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; $loggedInUsers3 = "SELECT * FROM techs WHERE Level>'0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; ?> <div class="col-sm-4"> <div class="thumbnail"> <center><h4 style="height:35px;">Users Online</h4></center> <div class="modal-body" style="min-height:498px;"> <table> <tr><td> <? $results1 = mysqli_query($con, $loggedInUsers2) or die("Error " . mysqli_error($con)); while($row2 = mysqli_fetch_array($results1)) { //if level less than 1 echo $row2['Name']."<br/>"; } ?> </td><td> <? $results2 = mysqli_query($con, $loggedInUsers3) or die("Error " . mysqli_error($con)); while($row3 = mysqli_fetch_array($results2)) { //if level more than 1 echo $row3['Name']."<br/>"; } ?> </td></tr> </table> </div> </div> </div>When you use mysqli_query it returns a result object. You need to pass this object into mysqli_fetch_array not the query string.
<? $loggedInUsers2 = "SELECT * FROM techs WHERE Level='0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; $loggedInUsers3 = "SELECT * FROM techs WHERE Level>'0' AND LastTimeSeen>DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; ?> <div class="col-sm-4"> <div class="thumbnail"> <center><h4 style="height:35px;">Users Online</h4></center> <div class="modal-body" style="min-height:498px;"> <table> <tr><td> <? $results1 = mysqli_query($con, $loggedInUsers2) or die("Error " . mysqli_error($con)); while($row2 = mysqli_fetch_array($results1)) { //if level less than 1 echo $row2['Name']."<br/>"; } ?> </td><td> <? $results2 = mysqli_query($con, $loggedInUsers3) or die("Error " . mysqli_error($con)); while($row3 = mysqli_fetch_array($results2)) { //if level more than 1 echo $row3['Name']."<br/>"; } ?> </td></tr> </table> </div> </div> </div>更多推荐
发布评论