Forum Moderators: coopster
Can anyone help me decipher why it won't allow me to search query with words that appear in separate table columns?
----> BEGIN CODE
if ($search) // performs search only if a search string was entered.
{
mysql_connect() or die ("Problem connecting to Database");
$search = trim($_POST['search']);
$sql= mysql_query("SELECT wname, title, keywords, description, city, country, category, url FROM directory WHERE city LIKE '%$search%' ¦¦ wname LIKE '%$search%' ¦¦ title LIKE '%$search%' ¦¦ keywords LIKE '%$search%' ¦¦ description LIKE '%$search%' ¦¦ country LIKE '%$search%' ¦¦ category LIKE '%$search%' ¦¦ url LIKE '%$search%'", $db);
<---- END CODE
~Shane
p.s. it also works with 'OR' in the place of '¦¦'
DID NOT WORK, FULL TEXT SEARCH// $sql= mysql_query("SELECT wname, title, keywords, description, city, country, category, url, MATCH (wname, title, keywords, description, city, country, category, url) AGAINST $search FROM directory", $db);
DID NOT WORK, FULL TEXT SEARCH// $sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory MATCH (wname, url, title, keywords, description, city, country, category) AGAINST ('$search')", $db);
DID NOT WORK, FULL TEXT SEARCH// $sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory MATCH (wname, url, title, keywords, description, city, country, category) AGAINST '%$search%'", $db);
DID NOT WORK, FULL TEXT SEARCH// $sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory MATCH (wname, url, title, keywords, description, city, country, category) AGAINST ('%$search%')", $db);
AM I doing these FullText Searches incorrectly?
~Shane
$search = trim(str_replace(" ","%",$_POST['search']));
This will get you a rudimentary way of handling multi-word searches.
Another way is to split up words by exploding them and then building your query that way.
$search = trim($_POST['search']);
if (ereg(" ",$search))
{
$Words = explode(" ",$search);
foreach($Words as $Word)
{
$sql .= " field like '%$Word%' or";
}
}
It's just an example, but it hopefully gives an idea on how to split up words and build a (rather long) query.
If that doesn't suit you, definitely check out full text searches.
It did not fully work. I have tried the following code. However, it only searches or shows results for the last word of the search string.
i.e.: search string: (john smith chicago)
* ONLY shows the search results for the word chicago in the city column from the table directory.
-------------> Begin Code
<?
include("inc/*****.php");
if(isset($_POST['search']) &&!empty($_POST['search'])) {
mysql_connect() or die ("Problem connecting to Database");
$search = trim($_POST['search']);
$search = explode(" ",$search);
foreach($search as $searches) {
$sql= mysql_query("SELECT wname, title, keywords, description, city, country, category, url FROM directory WHERE wname LIKE '%$searches%' OR title LIKE '%$searches%' OR keywords LIKE '%$searches%' OR description LIKE '%$searches%' OR city LIKE '%$searches%' OR country LIKE '%$searches%' OR category LIKE '%$searches%' OR url LIKE '%$searches%'", $db);
}
<------------- End Code
So I need to get it to search all the words not just the last word in the search string.