简体   繁体   中英

Variable in PHP MYSQL query issue

I have an SQL query in PHP where I am trying to pass a number into the where clause. If I pass the variable it doesn't work, however if I enter the variable manually it does work.

I tried converting to int and it still won't work.

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

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

Have I forgotten something?

Maybe You have just used dot so

$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";

Check this.

PS you should use prepared statements.

Datetime in where clause.

In datetime would have "" or ''

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

or

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

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