0

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";
2
  • 1
    you need a space before GROUP. and please for the question properly Commented Dec 11, 2017 at 4:44
  • 1
    Thank you made the change and still gett error. My variable is set to lenderName = 'Royal Bank of Canada'; and the error message drops the first word for some reason. Commented Dec 11, 2017 at 4:46

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0

please given one space between name and group

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.