Search DB by more than one word

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:
==============
<?php
$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 "<div class=\"message\">";
print " <h3 class=\"red\">" . $row->itdate . "</h3>";
print " <h5 class=\"red\">" . $row->itname . "</h5>";
print " <h4 class=\"red\">" . $row->itcomments ."</h4>";
print "############### END OF RECORD ###############";
print "</div>";
}
?>

thanks for any help
paul814 [ Mo, 07 Januar 2008 18:17 ] [ ID #1901974 ]

Re: Search DB by more than one word

paul814 [at] 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:
> ==============
> <?php
> $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 "<div class=\"message\">";
> print " <h3 class=\"red\">" . $row->itdate . "</h3>";
> print " <h5 class=\"red\">" . $row->itname . "</h5>";
> print " <h4 class=\"red\">" . $row->itcomments ."</h4>";
> print "############### END OF RECORD ###############";
> print "</div>";
> }
>?>
>
> 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".
Paul Lautman [ Mo, 07 Januar 2008 18:33 ] [ ID #1901976 ]

Re: Search DB by more than one word

On Jan 7, 12:33 pm, "Paul Lautman" <paul.laut... [at] btinternet.com>
wrote:
> paul... [at] 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:
> > ==============
> > <?php
> > $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 "<div class=\"message\">";
> > print " <h3 class=\"red\">" . $row->itdate . "</h3>";
> > print " <h5 class=\"red\">" . $row->itname . "</h5>";
> > print " <h4 class=\"red\">" . $row->itcomments ."</h4>";
> > print "############### END OF RECORD ###############";
> > print "</div>";
> > }
> >?>
>
> > 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?...
paul814 [ Mo, 07 Januar 2008 18:39 ] [ ID #1901979 ]

Re: Search DB by more than one word

On Jan 7, 12:39 pm, paul... [at] excite.com wrote:
> On Jan 7, 12:33 pm, "Paul Lautman" <paul.laut... [at] btinternet.com>
> wrote:
>
>
>
> > paul... [at] 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:
> > > ==============
> > > <?php
> > > $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 "<div class=\"message\">";
> > > print " <h3 class=\"red\">" . $row->itdate . "</h3>";
> > > print " <h5 class=\"red\">" . $row->itname . "</h5>";
> > > print " <h4 class=\"red\">" . $row->itcomments ."</h4>";
> > > print "############### END OF RECORD ###############";
> > > print "</div>";
> > > }
> > >?>
>
> > > 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
paul814 [ Mo, 07 Januar 2008 18:42 ] [ ID #1901980 ]

Re: Search DB by more than one word

paul814 [at] excite.com wrote:
> On Jan 7, 12:39 pm, paul... [at] excite.com wrote:
>> On Jan 7, 12:33 pm, "Paul Lautman" <paul.laut... [at] btinternet.com>
>> wrote:
>>
>>
>>
>>> paul... [at] 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
gn501957 [ Sa, 12 Januar 2008 03:54 ] [ ID #1906248 ]
PHP » comp.lang.php » Search DB by more than one word

Vorheriges Thema: Populating a form with data from a database
Nächstes Thema: changing indentation with DOMDocument