2012-03-23 47 views
1

這將是數據庫表的例子:PHP MySQL的 - 通過時間差限制查詢結果

enter image description here

PHP腳本30秒

要清楚前,應限制NOW和時間之間的查詢結果:

在目前的時間是2012-03-23 03:28:00和數據庫作爲例子descibed,resoult應該是: 「客人」

這是PHP腳本的一部分。

<?php 
    $con = mysql_connect("localhost", "user", "pass"); 
    if (!$con) { 
     die('Could not connect: '.mysql_error()); 
    } 

    mysql_select_db("db", $con); 

    $result = mysql_query('SELECT * FROM table LIMIT BY ...???...... '); 
     while ($row = mysql_fetch_array($result)){ 
     echo $row['username']; 
     } 

    mysql_close($con); 
?> 

回答

4

你不想LIMIT,你想有一個WHERE

$result = mysql_query(
'SELECT * FROM table WHERE `timestamp` > NOW()- INTERVAL 30 SECOND ' 
); 

在SQL中,LIMIT條款執行整個查詢,然後只需要連續行的某個「範圍」。你可以說「前50」,或者「從20到30」。

A WHERE子句限制對特定條件(如字段內容)的查詢。這就是你想要的。

+0

感謝您的澄清 – enloz 2012-03-23 09:26:21

2
... WHERE (unix_timestamp(NOW()) - unix_timestamp(timestamp))<30 .. 

OR

WHERE TIME_TO_SEC(TIMEDIFF(NOW(), timestamp))<30 
1
$result = mysql_query('SELECT * FROM table where date_sub(now(), timestamp) < your_time_interval ');