Jump to content

Easiest way to make this MySQL search work...


Mardoxx

Recommended Posts

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.