0

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

1
  • userid is input variable ? What do you mean by that? What programming language do you use? Commented Jul 25, 2014 at 14:08

3 Answers 3

4

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' "; 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. I just copy/paste you suggested, but still error
QUERY: SELECT * FROM login WHERE user_id LIKE 'userid'
java.sql.SQLException: Illegal operation on empty result set.
Do we think that this is a case sensitive collation?
"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" - I might quote you on that someday ;) or, have I just done that now? ;)
0

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

Comments

0

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'

1 Comment

dennisschagt, Thank you very very much!!!, I got passed by your way. Actually, John's way has generated same SQL query as yours, but still fail when debug, that's weird. Also thanks Prinsig's kind help, have a nice day you guys!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.