Urgent help please on search query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Urgent help please on search query

Post 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 :cry: :cry: :cry: :cry: :twisted:

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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' ...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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...
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

AGHHHH

Post 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

Code: Select all

<?php

for ($x > $i; $x++)

?>
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
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

PS

Post by rp »

Ps I dont actually need to offer the facility to match the whole string or individual words.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

oopsie

Code: Select all

<?php
for ($x > $count; $x++)
?>
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Thanks Phenom

Post 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)
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

use '$varname'
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

$row = mysql_fetch_array($result);
test this
Post Reply