简体   繁体   English

PHP MySQL比较日期是否相等

[英]php mysql comparing dates for equality

I am using php to access fields from 2 tables. 我正在使用php访问2表中的字段。

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" 我是菜鸟,对不起,如果此代码是如此“ 2000及之后”

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. 另外,不建议使用mysql_ *函数。 You need to look into using PDO or mysqli to query your database. 您需要研究使用PDO或mysqli查询数据库。

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: 大概您在查询中使用了标准的yyyy-mm-dd类型的日期字符串,这意味着您在日期值周围缺少引号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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