I am trying to include a WHERE %s LIKE in my db_query, but i end up with:


WHERE %s LIKE /"%%s%/" ...

How do you have a LIKE in a db_query...i'm presuming there must be a different way of doing it? TIA

Comments

cbow’s picture

You need to use %% to create a literal %.

Example:

db_query('select x from {table} where x like "%%%s%"', $string);

feo’s picture

Thanks bro, that was a strange hour to understand why some part of the alphabet didn't worked.

omendez85’s picture

thanks men

nevets’s picture

If you define the thing you are search for like

$pattern = '%' . $somevalue . '%';

and do your query something like

$results = db_query("SELECT .... WHERE field LIKE '%s'", $pattern);

you get rid of the need for the pairs of % signs.