Page 1 of 1

Searching Database with PHP

Posted: Thu Jul 30, 2009 3:18 pm
by Foxy999
I need to be able to search a row for a given string, but I am having trouble. This is what I have:

Code: Select all

 
$string = "toSearch";
while($row = mysql_fetch_array($result))
{
     if($row['name'] == $string)
     {
            echo "String found!";
     }
}
 
I need to add code so that if there are several words in the database and the $string matches at least one of the words then it will echo "String found!"


Foxy

Re: Searching Database with PHP

Posted: Thu Jul 30, 2009 4:14 pm
by jackpf
That's a very inefficient way of going about this.

You should use the SQL LIKE clause instead.

Re: Searching Database with PHP

Posted: Thu Jul 30, 2009 4:27 pm
by Foxy999
So something like this:

"SELECT * FROM X LIKE ".$string."%"

for my query, I guess that will work.

Thanks for the help,

Foxy

Re: Searching Database with PHP

Posted: Thu Jul 30, 2009 4:30 pm
by jackpf
The LIKE part goes in the WHERE section.

So, no like this:

Code: Select all

SELECT * FROM TABLE WHERE FIELD LIKE '%$string%'