一个困惑我很久的简单问题
$nian = 1990;
$a = ($nian-1);
$b = ($nian+1);
$query = "SELECT * FROM `final` WHERE `writedate` <'$b' and 'writedate'>'$a'";
$result = mysql_query("$query");
$row = mysql_fetch_array($result);
print_r($row);
上面这样写,就能输出正确结果,下面这样写,每次就什么都没有输出
$nian = 1990;
$query = "SELECT * FROM `final` WHERE `writedate` <'($nian+1)' and 'writedate'>'($nian-1)'";
$result = mysql_query("$query");
$row = mysql_fetch_array($result);
print_r(row);
writedate是date类型,不过之前也有这类似的情况,之前的字段是tinyint型,问题是多一个变量出来存储,例如上面的$a = ($nian-1);和直接进行加减,例如$nian-1,有什么区别
------解决方案--------------------`writedate` <'($nian+1)'
'($nian+1)' 是字符串,如何能进行运算呢
------解决方案--------------------$query = "SELECT * FROM `final` WHERE `writedate` <'($nian+1)' and 'writedate'>'($nian-1)'";
这样写($nian+1)根本不会被解析
------解决方案--------------------SQL语句成了…1990+1…1990-1…… 而非你期望的1991 1989,你仔细想想呢
------解决方案--------------------补充:
` WHERE `writedate` <'($nian+1)' and 'writedate'>'($nian-1)'";
如果改成这样,` WHERE `writedate` <($nian+1) and 'writedate'>($nian-1)";是能得到预期结果的
多出的''会被作为整体字符串对待,连同括号,
------解决方案--------------------尽量在sql语句中减少运算;下面貌似可以
PHP code
$query = "SELECT * FROM `final` WHERE `writedate` <'".($nian+1)."' and 'writedate'>'".($nian-1)."'";
------解决方案--------------------
------解决方案--------------------