user3837183
user3837183

Reputation: 1

MySQL SQL Syntax Issue

I am using a simplest SQL clause to retrieve user id,

String query = "SELECT * FROM `login` WHERE `user_id` LIKE `userid` "; 

in here, user_id is column, userid is input variable and not null,

when debug, system always report following errors,

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'userid' in 'where clause'

I can print userid as Allan, userid is String type and current value is "Allan". Why system unable to compare column and input variable?

Thank you very much in advance!

Rosemary

Upvotes: 0

Views: 46

Answers (3)

Dennis van der Schagt
Dennis van der Schagt

Reputation: 2414

John Conde's answer already resolves the error, but I have a small remark.

It looks like you want to select all login information from users with a user_id that is the same as the userid variable in your java code. To do that you can use:

String userid = "Rosemary";
String query = "SELECT * FROM `login` WHERE `user_id` LIKE '" + userid + "' ";

The actual query then becomes:

SELECT * FROM `login` WHERE `user_id` LIKE 'Rosemary'

Upvotes: 0

Prinsig
Prinsig

Reputation: 245

For the input variable, use '' not ``. I believe SQL interprets them as part of the system rather than quotations.

Upvotes: 0

John Conde
John Conde

Reputation: 219894

You need to place your string value in quotes, not ticks. By placing it in ticks you are telling MySQL it is a column identifier and not a string value like you want

String query = "SELECT * FROM `login` WHERE `user_id` LIKE 'userid' "; 

Upvotes: 4

Related Questions