Page 1 of 1
Urgent help please on search query
Posted: Sun Dec 12, 2004 2:41 pm
by rp
I am having great trouble writing a correct search query and need some help please.
User inputs text (more than 1 word i.e. a phrase) into an input box and hits a submit button.
I want a query that will take the pharse and create a search based on it's main words (I have tried Match() but so far to no avail)
the idea is that a word OR words in the pharse entered can match part of the text in a single DB field.
so far I have a variable $search (for the search text entry)
Code: Select all
<?php
$search = $_POST['search'];
$sqlsearch = "SELECT * FROM (table) WHERE MATCH (field) AGinst $search";
$rs = mysql_query($sqlsearch)
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
echo $row['Field'];
echo "<br>";
}
?>
BUT IT DOESN@T WORK
Please any help out there - I don't mind any other way of doing it BUT it must work, and it must be understandable by me - a complete novice!!!
Any help much appreciated - thanks
Posted: Sun Dec 12, 2004 2:57 pm
by John Cartwright
try using LIKE instead of match
Code: Select all
"SELECT * FROM `table` WHERE LIKE '$search'
This will match any occurances withen the rows
Posted: Sun Dec 12, 2004 3:10 pm
by rp
Like %search% is OK but will that not only just search on the "WHOLE" phrase rather tahn break the phrase up into words;
e.g a phrase may be "labrador dogs in wales" I need the search to match aginst the main words of dogs, labrador, wales and not the complete phrase as it is entered, i.e. if there is a match against labrador but not gainst dogs or wales then the query would fetch that record
Posted: Sun Dec 12, 2004 3:21 pm
by Roja
rp wrote:Like %search% is OK but will that not only just search on the "WHOLE" phrase rather tahn break the phrase up into words;
e.g a phrase may be "labrador dogs in wales" I need the search to match aginst the main words of dogs, labrador, wales and not the complete phrase as it is entered, i.e. if there is a match against labrador but not gainst dogs or wales then the query would fetch that record
Then you need to programmatically break the query into multiple "words", and then do a match for each like..
Code: Select all
SELECT * FROM `table` WHERE like '$word1' OR like '$word2' ...
Posted: Sun Dec 12, 2004 5:07 pm
by John Cartwright
and of course, have something like the following -- UNTESTED
Code: Select all
<?php
//assumming field is named search
$search = explode(" ",$_POST['search');
//get number of words
$count = count($search);
$sql = "SELECT * FROM `table` WHERE `content` ";
for ($x > $i; $x++)
{
//assumming there is a field type so user can choose if they want to match all or any words
switch ($_POST['type'])
{
//if user wants to match all words
case 'matchall':
if ($i == $count
? $sql .= "LIKE $item OR"
: $sql .= "LIKE $item "
);
break;
//if user wants to match any word
case 'any':
if ($i == $count
? $sql .= "LIKE $item AND"
: $sql .= "LIKE $item "
);
break;
}
}
?>
This isn't the greatest way to do it, but I want to give you an idea of how to
Posted: Sun Dec 12, 2004 5:12 pm
by ol4pr0
Yep i would go with the way phenom did it
to help u understand a bit what it is that that did.
run this
Code: Select all
$string = "this is a sentence";
$words = explode(" ", $string);
print_r($words);
#or
echo $words[0];
echo $words[1];
#ect...
AGHHHH
Posted: Mon Dec 13, 2004 2:57 am
by rp
Sorry guys I'm really stuck on this one
I have tried running Phenom's code and I get an error on line 8
I have used ol4pr0's code and can see the array being split up into [1], [2] etc, but I just cannot get any answer from the DB when I put in a query like Phemon's code.
Any help REALLY appreciated as I am trying to finish a project asap.
Thanks
PS
Posted: Mon Dec 13, 2004 3:01 am
by rp
Ps I dont actually need to offer the facility to match the whole string or individual words.
Posted: Mon Dec 13, 2004 6:30 pm
by John Cartwright
Thanks Phenom
Posted: Tue Dec 14, 2004 2:49 am
by rp
Thanks for post last night, sadly still get an error on the same line, error says unexpected ')', expecting ';' tried "all the ways" I can think of to remove the error but still keeps appearing.
Also which mysql_fetch should I use (if any)
Posted: Tue Dec 14, 2004 2:53 am
by itsmani1
use '$varname'
Posted: Tue Dec 14, 2004 2:56 am
by itsmani1
Code: Select all
$row = mysql_fetch_array($result);
test this