views:

18

answers:

1

I'm tring to convert a Mysql query to using a LIKE clause and I can't make it work.

$query = "SELECT id,name FROM `hin` WHERE name = '".$q."'";

What I've tried in some variations.

$query = "SELECT id,name FROM `hin` WHERE name LIKE %'".$q."'%";

I need the query to select row only on string match. Intend is to use variable as needle.

+3  A: 

Use:

"SELECT id,name FROM `hin` WHERE name LIKE '%". $q ."%'"

The wildcarding has to be inside the single quotes.

Ideally, you want to use:

"SELECT id,name FROM `hin` WHERE name LIKE '%". mysql_real_escape_string($q) ."%'"
OMG Ponies
@OMG Ponies Thanks so much!!
Codex73