Reputation: 709
Using SQL to match characters in query string
MySQL SELECT query string matching These do not match my requirement.
Hi, I am using PHP with mysql. I have a 'job search' functionality in my website, where we can search jobs by city. For example if we post a job in Hyderabad city, and if a user search for the job by using the same word 'Hyderabad' he gets the results. But if he searches by typing Hydrabad('e' is missing) he cannot get the results and also same problem with 'Delhi', 'New Delhi', 'NewDelhi'. If I use wild card operators like,
"SELECT * FROM jobs_tbl WHERE job_city LIKE '%".$search_city."%' "
"SELECT * FROM jobs_tbl WHERE job_city LIKE '%%".$search_city."%%' "
they wont work for me because I shoud get results though some letters and spaces are missed in the string. So, can you please tell me if there is any possibility to fetch a row if atleast 4 or 5 characters are matched with the search string? I searched for such operator but could not find any. Or is there any alternative way?
Upvotes: 1
Views: 1674
Reputation: 782653
You can put %
between each character.
$like_city = preg_replace('//', '%', $search_city); // Hyderbad => %H%y%d%e%r%b%a%d%
$sql = "SELECT * FROM jobs_tbl WHERE job_city LIKE '$like_city' "
Upvotes: 2