Search code examples
phpmysqlsearchsql-like

PHP MySql search with LIKE matches too many rows


I've taken over a site which uses a LIKE query to search in a field of delimited values. A typical field might look like this:

129|145|181

And the query looks like this:

SELECT id,title FROM research WHERE members like '$arr[0]'

The problem is that when this searches for, e.g. "29", it returns fields with "129" in them. I want it to only return fields with "29" in them. Is there any way of doing this?

Thanks, G


Solution

  • SELECT id,title FROM research WHERE members like '%|$arr[0]|%' OR members like '$arr[0]|%' OR members like '%|$arr[0]'