PHP中的变量MYSQL查询不起作用

时间:2018-01-30 06:20:28

标签: php mysql sql

我在PHP中有一个SQL查询,我试图将一个数字传递给where子句。如果我传递变量它不起作用,但是如果我手动输入变量,它确实有效。

我尝试过转换为int,但仍然无效。

 $numYear = '2017';
 $numMonth = '3';

 $query_select = "SELECT url, summary, page_name, created_date, EXTRACT(YEAR FROM created_date) AS numYear, EXTRACT(MONTH FROM created_date) AS numMonth, MONTHNAME(created_date) AS nameMonth FROM pages WHERE (url LIKE 'news/%' AND url NOT LIKE 'news/') AND (YEAR(created_date) = '.$numYear.' AND MONTH(created_date) = '.$numMonth.' ) AND active = 1 ORDER BY created_date DESC LIMIT 1";

我忘记了什么吗?

2 个答案:

答案 0 :(得分:2)

也许你刚刚使用过点

$query_select = "SELECT url, summary, page_name, created_date, EXTRACT(YEAR FROM created_date) AS numYear, EXTRACT(MONTH FROM created_date) AS numMonth, MONTHNAME(created_date) AS nameMonth FROM pages WHERE (url LIKE 'news/%' AND url NOT LIKE 'news/') AND (YEAR(created_date) = '$numYear' AND MONTH(created_date) = '$numMonth' ) AND active = 1 ORDER BY created_date DESC LIMIT 1";

检查一下。

P.S你应该使用准备好的陈述。

答案 1 :(得分:0)

where子句中的日期时间。

在datetime中会有""''

$DATE = "2017-12-31"
'SELECT * FROM Table WHERE date ='".$DATE."'ORDER BY ASE'

$DATE = "'".2017-12-31."'"
'SELECT * FROM Table WHERE date ='.$DATE