Home > Backend Development > PHP Tutorial > 一个困惑小弟我很久的简单有关问题

一个困惑小弟我很久的简单有关问题

WBOY
Release: 2016-06-13 13:45:35
Original
800 people have browsed it

一个困惑我很久的简单问题
$nian = 1990;
$a = ($nian-1);
$b = ($nian+1);
$query = "SELECT * FROM `final` WHERE `writedate` '$a'";  
$result = mysql_query("$query");
$row = mysql_fetch_array($result);

  print_r($row);
上面这样写,就能输出正确结果,下面这样写,每次就什么都没有输出
  $nian = 1990;
$query = "SELECT * FROM `final` WHERE `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)' 是字符串,如何能进行运算呢

------解决方案--------------------
$query = "SELECT * FROM `final` WHERE `writedate` '($nian-1)'";
这样写($nian+1)根本不会被解析

------解决方案--------------------
SQL语句成了…1990+1…1990-1…… 而非你期望的1991 1989,你仔细想想呢
------解决方案--------------------
补充:

` WHERE `writedate` '($nian-1)'";

如果改成这样,` WHERE `writedate` ($nian-1)";是能得到预期结果的

多出的''会被作为整体字符串对待,连同括号,
------解决方案--------------------
尽量在sql语句中减少运算;下面貌似可以

PHP code

$query = "SELECT * FROM `final` WHERE `writedate` '".($nian-1)."'";
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

原来MYSQL不能进行转换的啊,无语了,不过还好,东西做出来交差了,不用弄这些我不会的了,谢谢了引用:
SQL语句成了…1990+1…1990-1…… 而非你期望的1991 1989,你仔细想想呢

------解决方案--------------------
探讨

理由呢?我好像见到书上也常常用括号的引用:
$query = "SELECT * FROM `final` WHERE `writedate` '($nian-1)'";
这样写($nian+1)根本不会被解析
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template