user7748962
user7748962

Reputation:

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

Upvotes: 0

Views: 60

Answers (3)

Nazish Fraz
Nazish Fraz

Reputation: 94

please given one space between name and group

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

Upvotes: 0

Pupil
Pupil

Reputation: 23958

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.

Upvotes: 5

oreopot
oreopot

Reputation: 3450

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

Upvotes: 3

Related Questions