Mardoxx Posted July 27, 2009 Share Posted July 27, 2009 Say, I have a table full of names and some random data about them (for example) Table: names Names Data Adam Cell 2565465467 Bob Toffee 3455767 Charlie Smith 33248989896324 What would be the best way of writing an SQL query to search through this with multiple search criteria? EG. I want a search query of "da Smit" to yield the following results: Search results Names Data Adam Cell 2565465467 Charlie Smith 33248989896324 ----------------------- I though of doing it like this, have the search criteria separated by a comma. So my search query would be "da,Smit" Then using code like this: <?php include('connect.php'); $search_query = "da,Smit"; $search_array = split(",",$search_query); $query = "SELECT Names, Data FROM names WHERE Name LIKE '%$search_array[0]%'"; foreach ($search_array as $search) { $query .= "'%$search%'" } $count = count($search_array) - 1; for ($i = 1; $i <= $count; $i++) { $query .= "OR WHERE Name LIKE '%$search[$i]%'" } //DO QUERY ON DATABASE HERE ?> But that ^^^^ is ABSOLUTELY awful.... so is my method of searching/inputting the search.... would anyone be able to help me please? thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/167663-easiest-way-to-make-this-mysql-search-work/ Share on other sites More sharing options...
DaiLaughing Posted July 28, 2009 Share Posted July 28, 2009 Firstly the two names should be in separate fields but assuming you have reasons... If there will only be two search terms then use an OR in the WHERE. Quote Link to comment https://forums.phpfreaks.com/topic/167663-easiest-way-to-make-this-mysql-search-work/#findComment-884759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.