![](/img/trans.png)
[英]php - pulling data from one column mysql DB not sure what I'm doing wrong here?
[英]Check if user is online in PHP: What I'm doing wrong here?
function checkUserStatus($uid) {
$q = "select last_activity from signup_and_login_table where id='$uid' LIMIT 1";
$link=mysql_connect("localhost","mydbusr","mydbpass");
mysql_select_db("mymaindb",$link);
$ros=mysql_query($q,$link);
while($row=mysql_fetch_array($ros))
{
$last_activity = $row['last_activity'];
if (strtotime(date('Y-m-d H:i:s')) > strtotime($last_activity) + 30 ) {
return false;
} else {
return true;
}
}
}
每當用戶刷新頁面或執行其他操作時,我都會更新mysql的last_activity值。
為什么即使last_activity和now()之間的時間差超過30秒,上面的函數也始終返回true,而從不返回false?
mysql中的時間以以下格式表示:2014-05-02 07:44:55
任何提示將不勝感激
如果要使用“ while循環”,則應使用mysql_fetch_assoc()函數作為下一個:
function checkUserStatus($uid) {
$q = "select last_activity from signup_and_login_table where id='$uid'";
$link=mysql_connect("localhost","mydbusr","mydbpass");
mysql_select_db("mymaindb",$link);
$ros=mysql_query($q,$link);
while($row=mysql_fetch_assoc($ros))
{
$last_activity = $row['last_activity'];
if (strtotime(date('Y-m-d H:i:s')) > strtotime($last_activity) + 30 ) {
return false;
} else {
return true;
}
}
}
或者,您可以嘗試不使用循環:
function checkUserStatus($uid) {
$q = "select last_activity from signup_and_login_table where id='$uid' LIMIT 1";
$link=mysql_connect("localhost","mydbusr","mydbpass");
mysql_select_db("mymaindb",$link);
$ros=mysql_query($q,$link);
$row = mysql_fetch_assoc($ros);
$last_activity = $row['last_activity'];
if (strtotime(date('Y-m-d H:i:s')) > strtotime($last_activity) + 30 ) {
return false;
} else {
return true;
}
}
您應該將其更改為:
function checkUserStatus($uid) {
$q = "select last_activity from signup_and_login_table where id='$uid' ORDER BY last_activity DESC LIMIT 1";
$link=mysql_connect("localhost","mydbusr","mydbpass");
mysql_select_db("mymaindb",$link);
$ros=mysql_query($q,$link);
$row=mysql_fetch_array($ros);
$last_activity = $row['last_activity'];
if (strtotime(date('Y-m-d H:i:s')) > strtotime($last_activity) + 30 ) {
return false;
} else {
return true;
}
}
您只能選擇1條記錄,但可以按last_activity DESC進行排序,並簡單地將其與當前時間進行比較
最好使用mysqli_*
函數。
function checkUserStatus($uid) {
$q = "select last_activity from signup_and_login_table where id='$uid' LIMIT 1";
$link = mysqli_connect("localhost","mydbusr","mydbpass");
mysqli_select_db("mymaindb",$link);
$res = mysqli_query($link, $q);
$row = mysqli_fetch_num($res);//this function returns the result in a numeric array
return time() > strtotime($row[0]) + 30;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.