时间戳比较MySQL PHP

时间:2011-09-17 22:15:00

标签: php mysql datetime comparison timestamp

我正在尝试将行与当前日期和小时进行比较,

    $curDate = date("Y-m-d H").':00:00';
    $query = "SELECT Id FROM Programacao WHERE Data1 = $curDate OR Data2 = $curDate OR Data3 = $curDate 
    OR Data4 = $curDate OR Data5 = $curDate OR Data6 = $curDate  OR Data7 = $curDate";

    $result = mysql_query($query);   
    if(!$result) {echo 'No program today';}
    while ($row = mysql_fetch_array($result))
    {
        echo $row['Id'];
    }

数据库中的行数据如下:“2011-09-10 18:00:00” 但我总是得到:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result 任何更正?

2 个答案:

答案 0 :(得分:1)

您需要在SQL中围绕日期文字引用:

"... WHERE Data1 = '$curDat' ..."

生成的SQL看起来像这样:

... WHERE Data1 = '2010-12-17 15:00:00' ...

答案 1 :(得分:0)

$result = mysql_query($query) or die(mysql_error());   

会给你错误,知道出了什么问题

在你的情况下,你忘记了约会的引号

这个更好,可能会有效

$query = "SELECT Id FROM Programacao WHERE Data1 = '$curDate' OR Data2 = '$curDate' OR Data3 = '$curDate' 
OR Data4 = '$curDate' OR Data5 = '$curDate' OR Data6 = '$curDate'  OR Data7 = '$curDate'";