简体   繁体   中英

php mysql comparing dates for equality

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"

Assuming both date fields are of type date, you need to wrap the name date in backticks, since date is a reserved word and you need to wrap your date in quotes.

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

Also, mysql_* functions are deprecated. You need to look into using PDO or mysqli to query your database.

date是保留字,用于包装反引号`date

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

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 . '"');
            ^--               ^--

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM