Connecting Tech Pros Worldwide Help | Site Map

What is the correct syntax to use MySQL LIKE query with parameters?

Newbie
 
Join Date: Sep 2009
Posts: 1
#1: Sep 3 '09
Hi All,

I am trying to query a mysql database using the 'LIKE' keyword within my query based on what the user has input.

It works if I pass the string, however I am having trouble getting it to expand the parameter when combining it with the LIKE MySQL keyword.

As seen below parameters work in a simple SELECT without 'LIKE':

Expand|Select|Wrap|Line Numbers
  1. string sql2 = "SELECT * FROM vocab WHERE english LIKE '%" + english + "%'";
  2. string sql3 = "SELECT * FROM vocab WHERE english LIKE '%" + "@English" + "%'";
  3. string sql4 = "SELECT * FROM vocab WHERE english =@English";
sql2 and sql4 both work and return data when entered through my application window. However sql3 doesn't as it doesn't seem to expand @English into whatever I have entered.

The single quotes around the % symbols are probably the cause but I can't figure it out.

Could someone please tell me the correct syntax for the statement in sql3?
Reply