修改:已修复
我正在用mysql和php编写一个用户计数器,这将在10分钟内将所有连接的用户更新为一个表,每隔10分钟,它将删除该表中的所有记录。
我的表就像:
id (int,auto increment)| 会话(varchar)| 时间(int)
但是我用来删除记录的查询不正确,它不起作用。有人帮助我,PLZ !!!
<?php
$session = session_id();
$time = time();
$time_check = $time - 900;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user";
$tbl_name = "useronline";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Update session
$sql = "SELECT * FROM $tbl_name WHERE session='$session'";
$result = $conn->query($sql);
$count = $result->num_rows;
if ($count == "0") {
$sql1 = "INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1 = $conn->query($sql1);
} else {
$sql2 = "UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2 = $conn->query($sql2);
}
// Count session online
$sql3 = "SELECT * FROM $tbl_name";
$result3 = $conn->query($sql3);
$count_user_online = $result3->num_rows;
// Delete session
$sql4 = 'DELETE FROM useronline WHERE time<$time_check';
$result4 = $conn->query($sql4);
// Total session
$sql5 = 'SELECT id FROM useronline ORDER BY id DESC LIMIT 1';
$result5 = $conn->query($sql5);
$row = $result5->fetch_assoc();
$total = $row['id'];
mysqli_close($conn);
?>
输出:
User online: <?php echo $count_user_online; ?><br />
Total : <?php echo $total; ?>
答案 0 :(得分:0)
我不明白你删除查询,为什么你在做一个不在?应该是这样的:'DELETE FROM useronline WHERE time<$time_check';
?