2011-08-31 56 views
0

我希望$minutes在查詢中以最短的datecommented(以分鐘表示)。當我回顯$minutes時,我得到一個空白結果。我究竟做錯了什麼?從查詢中發現日期

$queryuidcount = "SELECT loginid, datecommented 
    FROM comment 
    WHERE (HOUR(NOW()) - HOUR(datecommented)) <= 1 
    AND loginid = '$uid' 
ORDER BY datecommented ASC 
    LIMIT 1'"; 
$row2 = mysql_fetch_array($queryuidcount); 
$minutes = $row2["datecommented"]; 

回答

0

你需要給你調用的結果mysql_fetch_array()之前您的查詢執行mysql_query()。試試這個:

$result = mysql_query('SELECT loginid, datecommented FROM comment WHERE (HOUR(NOW()) - HOUR(datecommented)) <= 1 AND loginid = '$uid' ORDER BY datecommented ASC LIMIT 1'); 

$row = mysql_fetch_array($result); 

$minutes = $row['datecommented']; 

如果查詢返回多行,你需要通過他們while循環迭代:

while($row = mysql_fetch_array($result)) { 
    //Fetch values from $row 
} 

而且,考慮使用PDO,而不是MySQL的函數族。