Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching Database PHP/MYSQL Question

Tags:

php

search

mysql

Right now I'm just using a simple

WHERE name LIKE '%$ser%'

But I'm running into an issue - say the search is Testing 123 and the "name" is Testing, it's not coming back with any results. Know any way to fix it? Am I doing something wrong?

like image 585
Belgin Fish Avatar asked Jul 04 '26 07:07

Belgin Fish


2 Answers

If you want to search for 'Testing' or '123' use OR:

WHERE (name LIKE '%Testing%' OR name LIKE '%123%')

Note however that this will be very slow as no index can be used and it may return some results you didn't want (like "4123"). Depending on your needs, using a full text search or an external database indexing product like Lucene might be a better option.

like image 180
Mark Byers Avatar answered Jul 05 '26 22:07

Mark Byers


That's how LIKE works - it returns rows that completely contain the search string, and, if you use "%" optionally contain something else.

If you want to see if the field is contained in a string, you can do it this way:

SELECT * FROM `Table` WHERE  "Testing 123" LIKE CONCAT("%",`name`,"%") 
like image 26
Scott Saunders Avatar answered Jul 05 '26 21:07

Scott Saunders