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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.