Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Parameterized Query using Like

Tags:

I want to run a query like this in MySQL:

select * from table where column1 like '%searchdata%' 

But I want to use a parameter to pass in the search text. Is this possible? This doesn't seem to work:

select * from table where column1 like '%?Parameter%' 
like image 618
Jon Tackabury Avatar asked Apr 21 '09 17:04

Jon Tackabury


People also ask

How to pass parameter in like operator in MySQL?

The query is as follows. mysql> select *from UserVariableInLike; The following is the output. mysql> set @searchName='John'; Query OK, 0 rows affected (0.00 sec) mysql> select id,Name,Age from UserVariableInLike where Name like CONCAT('%', @searchName, '%');

Does like work in mysql?

Does Ilike work in MySQL? It is in PostgreSQL the keyword ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension. In MySQL you do not have ILIKE.

What is parameterized query in mysql?

A parameterized query is a query in which placeholders are used for parameters and the parameter values are supplied at execution time. The most important reason to use parameterized queries is to avoid SQL injection attacks.


1 Answers

The % symbols need to be inside the parameter value, so it's something more like:

select * from table where column1 like ?; 

And then you set the parameter to:

%searchText% 
like image 91
Chad Birch Avatar answered Oct 13 '22 22:10

Chad Birch