0

I am implementing a search function on a web site.

There is a field called nombre_ciudad and a value for this field is "Ciudad Juárez".

I need to search for the term "Ju" and now it doesn't work.

This is the part of the code that I am using now:

$sql.=" OR nombre_ciudad LIKE '".$requestData['search']['value']."%' ";

The search works for terms like "Ciudad Juarez", "C", "ciudad", but not for "arez" or "rez"

mvasco
  • 4,965
  • 7
  • 59
  • 120

2 Answers2

3
  1. you are missing the wildcard character % at the start of the pattern
  2. you are vulnerable to SQL injection, see: How can I prevent SQL injection in PHP?
Community
  • 1
  • 1
Shira
  • 6,392
  • 2
  • 25
  • 27
1

The % symbol in MySQL acts as a wildcard. If I'm trying to search for ciudad, and I type in LIKE '"da%"', I would get back all entries that start with da, and have any ending after that, including dad. In order to search before and after your query, you need to place a wildcard before and after, like this:

OR nombre_ciudad LIKE '%da%';
Brian Powell
  • 3,336
  • 4
  • 34
  • 60