简体   繁体   中英

PHP MYSQL Variable returns error

Simple question, but my searching is not finding an answer. I am trying to use a variable in a MySQL search and I keep getting an error. I know the variable value is good because when I pass it directly it works. Here is the error I get

"Error: something went wrong: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Bank of CanadaGROUP BY YEAR(date), MONTH(date) DESC' at line "

$query = "SELECT * FROM current_rates WHERE 
financial_institution =". $lenderName .
"GROUP BY YEAR(date), MONTH(date) DESC";

Strings in MySQL must be enclosed with single quotes.

Otherwise, they will be considered as reserved words / column names .

The corrected SQL should be:

$query = "SELECT * FROM current_rates WHERE 
 financial_institution ='". $lenderName .
 "' GROUP BY YEAR(date), MONTH(date) DESC";

Also, as per answer from Bhaumik Mehta , you need to add a space before GROUP BY tag.

Try this query:

1) You need to enclose $lenderName in '' as the it is a string value.

2) You need to have space before GROUP BY keyword

$query = "SELECT * FROM current_rates WHERE financial_institution ='". $lenderName ."' GROUP BY YEAR(date), MONTH(date) DESC";

请在名称和组之间留一个空格

$query = "SELECT * FROM current_rates WHERE financial_institution ='".$lenderName."' GROUP BY YEAR(date), MONTH(date) DESC";

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