I am working with a MySQL database that contains information on over 8000 boats. The client searches for the boat's name on an HTML form and then results.php is supposed to search for boat names from the database that contain the client's search terms ($nameTerms) and display those boats and their associated information. Until now, I've used stristr() with selecting every record in the database as follows:
$pkresult=mysql_query("select * from packetsdb order by wayno");
while($row=mysql_fetch_array($pkresult))
{
if ($nameTerms!="")
{
if (stristr($row['name'],$nameTerms))
{
dosomestuff();
}
}
}
Instead of querying every record in the database, I'd like to add a WHERE clause to get something like this:
$pktermsresult=mysql_query("select * from packetsdb where name='$nameTerms'");
while($row=mysql_fetch_array($pktermsresult))
{
dosomestuff();
}
Unfortunately, this only returns records from the database where the name is exactly equivalent to the search terms, save for case-sensitivity. Is there any way to do something similar to stristr() in the WHERE clause, such that it will return records that match partially?
e.g., user searches for James. I want to return all the records containing those terms in the name, like James Allen, James McNamara, Jamestown, Ronald James.
Any help greatly appreciated.