Search Multiple Words PHP

时间:2015-05-24 21:15:43

标签: php mysql search

UPDATE:

I got a little farther with:

$query = mysql_real_escape_string($_POST['search']); 
$keywords = explode(" ", $query); 
foreach ($keywords as $keys) { 
$search_sql .= " AND Name LIKE '%$keys%' "; 
}

But it's still returning nothing.

I couldn't find this because I wasn't sure on how to word it. Basically, when a user queries my database I want to return the card, but if the user misses one word that's in the middle (say he queries Elesh Norn Foil, since they didn't query Elesh Norn, Grand Cenobite Foil exactly the Grand Cenobite trips my program up and returns nothing).

So:

-Johnny types "Elesh Norn" he returns "Elesh Norn, Grand Cenobite" and "Elesh Norn, Grand Cenobite Foil"

-However, if Johnny types "Elesh Norn Foil" it returns with a "No card found!" match

Here's my query statement:

SELECT Name, Amount, CardID FROM modernmasters WHERE Name LIKE '%".$_POST['search']."%'

So what would I have to change my query to so my search doesn't get tripped up?

1 个答案:

答案 0 :(得分:6)

试试这个。希望它会有所帮助。

$search_sql = "SELECT Name, Amount, CardID FROM modernmasters WHERE";
$query = mysql_real_escape_string($_POST['search']); 
$keywords = explode(" ", $query); 
$keyCount = 0;
foreach ($keywords as $keys) { 
    if ($keyCount > 0){
        $search_sql .= " AND";
    }
    $search_sql .= " Name LIKE '%$keys%'"; 
    ++$keyCount;
}