Search DB by more than one word

Search DB by more than one word

am 07.01.2008 18:17:32 von paul814

Hello everyone,
Right now I have a simple PHP page setup that takes whatever was
entered into txtsearch on the previous page, it searches my DB by that
word and brings back the whole record based on the word.

So if I enter: ALPHA
it will bring back this record: the alpha dog was there

HOWEVER if I enter: ALPHA DOG or alpha dog
(so if I enter more than one word) it does not find any records. How
can I fix this so that if I enter ALPHA DOG it will find the record:
the alpha dog was there

Here is my code:
==============
$host="localhost";
$user="root";
$pass="";
$db="productiondb";
$con = mysql_connect($host, $user, $pass);

$Search = $_POST["txtsearch"];

if (!$con)
{
die('Unable to connect: ' . mysql_error());
}
mysql_select_db($db, $con) or die('Unable to connect: ' .
mysql_error());


$sql = "SELECT * FROM IT WHERE itcomments LIKE '$Search%' ";
$rs = mysql_query($sql,$con);

while($row=mysql_fetch_object($rs)){

print "

";
print "

" . $row->itdate . "

";
print "
" . $row->itname . "
";
print "

" . $row->itcomments ."

";
print "############### END OF RECORD ###############";
print "
";
}
?>

thanks for any help

Re: Search DB by more than one word

am 07.01.2008 18:33:10 von Paul Lautman

paul814@excite.com wrote:
> Hello everyone,
> Right now I have a simple PHP page setup that takes whatever was
> entered into txtsearch on the previous page, it searches my DB by that
> word and brings back the whole record based on the word.
>
> So if I enter: ALPHA
> it will bring back this record: the alpha dog was there
>
> HOWEVER if I enter: ALPHA DOG or alpha dog
> (so if I enter more than one word) it does not find any records. How
> can I fix this so that if I enter ALPHA DOG it will find the record:
> the alpha dog was there
>
> Here is my code:
> ==============
> > $host="localhost";
> $user="root";
> $pass="";
> $db="productiondb";
> $con = mysql_connect($host, $user, $pass);
>
> $Search = $_POST["txtsearch"];
>
> if (!$con)
> {
> die('Unable to connect: ' . mysql_error());
> }
> mysql_select_db($db, $con) or die('Unable to connect: ' .
> mysql_error());
>
>
> $sql = "SELECT * FROM IT WHERE itcomments LIKE '$Search%' ";
> $rs = mysql_query($sql,$con);
>
> while($row=mysql_fetch_object($rs)){
>
> print "

";
> print "

" . $row->itdate . "

";
> print "
" . $row->itname . "
";
> print "

" . $row->itcomments ."

";
> print "############### END OF RECORD ###############";
> print "
";
> }
>?>
>
> thanks for any help

I'm amazed!
Looking at your code I would not expect searching on "alpha" to bring back
"the alpha dog was there", since this does not begin with the word "alpha".

Re: Search DB by more than one word

am 07.01.2008 18:39:53 von paul814

On Jan 7, 12:33 pm, "Paul Lautman"
wrote:
> paul...@excite.com wrote:
> > Hello everyone,
> > Right now I have a simple PHP page setup that takes whatever was
> > entered into txtsearch on the previous page, it searches my DB by that
> > word and brings back the whole record based on the word.
>
> > So if I enter: ALPHA
> > it will bring back this record: the alpha dog was there
>
> > HOWEVER if I enter: ALPHA DOG or alpha dog
> > (so if I enter more than one word) it does not find any records. How
> > can I fix this so that if I enter ALPHA DOG it will find the record:
> > the alpha dog was there
>
> > Here is my code:
> > ==============
> > > > $host="localhost";
> > $user="root";
> > $pass="";
> > $db="productiondb";
> > $con = mysql_connect($host, $user, $pass);
>
> > $Search = $_POST["txtsearch"];
>
> > if (!$con)
> > {
> > die('Unable to connect: ' . mysql_error());
> > }
> > mysql_select_db($db, $con) or die('Unable to connect: ' .
> > mysql_error());
>
> > $sql = "SELECT * FROM IT WHERE itcomments LIKE '$Search%' ";
> > $rs = mysql_query($sql,$con);
>
> > while($row=mysql_fetch_object($rs)){
>
> > print "

";
> > print "

" . $row->itdate . "

";
> > print "
" . $row->itname . "
";
> > print "

" . $row->itcomments ."

";
> > print "############### END OF RECORD ###############";
> > print "
";
> > }
> >?>
>
> > thanks for any help
>
> I'm amazed!
> Looking at your code I would not expect searching on "alpha" to bring back
> "the alpha dog was there", since this does not begin with the word "alpha".

Hmm, you are right, I didnt notice that, that was just an example,
not an actual query......hmm, yea if it does not begin with whatever I
search for I do not get a result?...

Re: Search DB by more than one word

am 07.01.2008 18:42:30 von paul814

On Jan 7, 12:39 pm, paul...@excite.com wrote:
> On Jan 7, 12:33 pm, "Paul Lautman"
> wrote:
>
>
>
> > paul...@excite.com wrote:
> > > Hello everyone,
> > > Right now I have a simple PHP page setup that takes whatever was
> > > entered into txtsearch on the previous page, it searches my DB by that
> > > word and brings back the whole record based on the word.
>
> > > So if I enter: ALPHA
> > > it will bring back this record: the alpha dog was there
>
> > > HOWEVER if I enter: ALPHA DOG or alpha dog
> > > (so if I enter more than one word) it does not find any records. How
> > > can I fix this so that if I enter ALPHA DOG it will find the record:
> > > the alpha dog was there
>
> > > Here is my code:
> > > ==============
> > > > > > $host="localhost";
> > > $user="root";
> > > $pass="";
> > > $db="productiondb";
> > > $con = mysql_connect($host, $user, $pass);
>
> > > $Search = $_POST["txtsearch"];
>
> > > if (!$con)
> > > {
> > > die('Unable to connect: ' . mysql_error());
> > > }
> > > mysql_select_db($db, $con) or die('Unable to connect: ' .
> > > mysql_error());
>
> > > $sql = "SELECT * FROM IT WHERE itcomments LIKE '$Search%' ";
> > > $rs = mysql_query($sql,$con);
>
> > > while($row=mysql_fetch_object($rs)){
>
> > > print "

";
> > > print "

" . $row->itdate . "

";
> > > print "
" . $row->itname . "
";
> > > print "

" . $row->itcomments ."

";
> > > print "############### END OF RECORD ###############";
> > > print "
";
> > > }
> > >?>
>
> > > thanks for any help
>
> > I'm amazed!
> > Looking at your code I would not expect searching on "alpha" to bring back
> > "the alpha dog was there", since this does not begin with the word "alpha".
>
> Hmm, you are right, I didnt notice that, that was just an example,
> not an actual query......hmm, yea if it does not begin with whatever I
> search for I do not get a result?...

I think I fixed this by going from this:
$sql = "SELECT * FROM EDITORIAL WHERE editorialcomments LIKE '$Search
%' ";

to this:
$sql = "SELECT * FROM EDITORIAL WHERE editorialcomments LIKE '%$Search
%' ";

And I think this fixed my initial problem, question

Re: Search DB by more than one word

am 12.01.2008 03:54:33 von gn501957

paul814@excite.com wrote:
> On Jan 7, 12:39 pm, paul...@excite.com wrote:
>> On Jan 7, 12:33 pm, "Paul Lautman"
>> wrote:
>>
>>
>>
>>> paul...@excite.com wrote:
<...>
> I think I fixed this by going from this:
> $sql = "SELECT * FROM EDITORIAL WHERE editorialcomments LIKE '$Search
> %' ";
>
> to this:
> $sql = "SELECT * FROM EDITORIAL WHERE editorialcomments LIKE '%$Search
> %' ";
>
> And I think this fixed my initial problem, question

Yep. You got it. I would only add that, for performance reasons, I'd
recommend that the editorialcomments column is being indexed. These
"fuzzy" searches, that use the % in the clause tend to be much slower.
As well as the fact that text (varchar()) columns are slower even if an
explicit clause were used.

--

Gene Kelley
Senior Open Source Software Engineer
Advanced Design Solutions Team
Network Solutions (MonsterCommerce)
Swansea, Illinois, USA
Eugene.Kelley_AT_networksolutions_DOT_com