PHP MYSQL变量返回错误

0 php mysql

简单的问题,但我的搜索没有找到答案.我试图在MySQL搜索中使用变量,我一直收到错误.我知道变量值是好的,因为当我直接传递它时它起作用.这是我得到的错误

"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";
Run Code Online (Sandbox Code Playgroud)

Pup*_*pil 5

MySQL中的字符串必须用单引号括起来.

否则,它们将被视为保留字/列名称.

更正的SQL应该是:

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

此外,根据Bhaumik Mehta的回答,您需要在GROUP BY标记之前添加空格.