donglun7151 2013-12-20 15:27
浏览 90
已采纳

php mysql比较相等的日期

I am using php to access fields from 2 tables.

This part works just fine

   $sql=mysql_query('SELECT * FROM user_weeks WHERE user_id = '.$_SESSION["user_id"].' ORDER BY date DESC') or die(mysql_error());

I get the date just fine by doing this

    $infodate=$info["date"];
    echo $infodate;

However I'm trying to take that date and compare it to one in a different table as such

$sql2=mysql_query('SELECT * FROM weekly_ROI WHERE date = '.$infodate.' ') or die(mysql_error());

however, that gives me no results. I'm a noob so sorry if this code is so "2000 and late"

  • 写回答

3条回答 默认 最新

  • doutou7549 2013-12-20 15:37
    关注

    Presumably you're using a standard yyyy-mm-dd type date string in your query, which means you're lacking quotes around the date value:

    $sql2=mysql_query('SELECT * FROM weekly_ROI WHERE date = '.$infodate.' ')
                                                             ^--here          ^-- here
    

    Your query will look like

    ... WHERE date = 2013-12-18
    

    and be evaluated as a simple mathematical subtraction:

    ... WHERE date = 1983
    

    You need quotes:

    .... date = "' . $infodate . '"');
                ^--               ^--
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?