Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search by multiple keywords

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
US
Hello,

I am using the following code to display a search from MySQL database. My problem is if I try to search database with several keywords, the search doesn't find any results. For example if I search for color and type "red" in my search field it would give me several results, but if I search for color and type red blue, it won't show any resolts whatsoever. How can I make my search to search for several keywords at the same time.

<html>
<head>
<title>Search Results</title>
</head>
<body>
<h1>Search Results</h1>
<?
if (!$searchtype || !$searchterm)
{
echo &quot;You have not entered search details. Please go back and try again.&quot;;
exit;
}

$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect(&quot;localhost&quot;, &quot;root&quot;, &quot;password&quot;);

if (!$db)
{
echo &quot;Error: Could not connect to database. Please try again later.&quot;;
exit;
}

mysql_select_db(&quot;catalog&quot;);
$query = &quot;select * from cars where &quot;.$searchtype.&quot; like '%&quot;.$searchterm.&quot;%'&quot;;
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo &quot;<p>Number of records found: &quot;.$num_results.&quot;</p>&quot;;

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo &quot;<p><strong>&quot;.($i+1).&quot;. Model: &quot;;
echo stripslashes($row[&quot;model&quot;]);
echo &quot;</strong><br>Color: &quot;;
echo stripslashes($row[&quot;color&quot;]);
echo &quot;<br>Year: &quot;;
echo stripslashes($row[&quot;year&quot;]);
echo &quot;<br>Price: &quot;;
echo stripslashes($row[&quot;price&quot;]);
echo &quot;</p>&quot;;
}

?>

</body>
</html>
 
I would imagine it's because of how you construct your query.

With the code you've posted, the only way you can get multiple column names into the where clause of the query is to put both names in the field &quot;searchtype&quot;. But that would construct a string like:

&quot;select * from cars where firstfield, secondfield like '%mysearchterm%'&quot;

However, this is not a valid SQL statement. To search for a single term across multiple fields, you need to built an SQL statement of the form:

&quot;select * from cars where firstfield like '%mysearchterm%' and secondfield like '%mysearchterm%'&quot;

Unfortunately, your script does not contain code which would trap an error which would be generated by such an ill-formed query. It simply reports that you didn't get any rows back.


Here's my standard advice for debugging MySQL database-connection code:

[ul][li]Trap every error it is possible to trap, and get in the habit of doing it every time. Make sure that you print out every error that MySQL presents to your code. This is the easiest way to know exactly where your code went wrong[/li][li]Verify that your query is well-formed. Print it out to the screen and examine it by hand. Does it look right to you?[/li][li]Cut that query from your browser and paste it into your favorite MySQL admin tool, then execute it. What does MySQL have to say about it?[/li][/ul]

______________________________________________________________________
TANSTAAFL!
 
I don't think you understood what I am trying to do. Lets say in the description of a car under color I have &quot;red, green, blue, yellow, etc.&quot; If I search for &quot;red&quot; for example, it will give me all resuls that have word &quot;red&quot; in it. But if I search &quot;red yellow&quot; for example it doesn't give me any results. This is because red and yellow don't appear next to each other in the database. However if I search for red green I will get all the results that have red green next to each other. However, what I am trying to do is to type any combination of words in any order and let my search find it in the database.

Here is how I constracted my search form (if that of any help)

<html>
<head>
<title>Catalog Search</title>
</head>

<body>
<h1>Catalog Search</h1>

<form action=&quot;results.php&quot; method=&quot;post&quot;>
Choose Search Type:<br>
<select name=&quot;searchtype&quot;>
<option value=&quot;model&quot;>Model
<option value=&quot;volor&quot;>Color
<option value=&quot;year&quot;>Year
<option value=&quot;price&quot;>Price
</select>
<br>
Enter Search Term:<br>
<input name=&quot;searchterm&quot; type=text>
<br>
<input type=submit value=&quot;Search&quot;>
</form>

</body>
</html>
 
To perform a case-insensitive single-word-term search that assumes you are always looking for <term> or <term>, convert any incidences of one or more non-word characters to &quot;|&quot;. Then construct your query as:

SELECT * FROM cars WHERE lower(&quot;.$searchtype.&quot;) regex '&quot;.strtolower($searchterm).&quot;'&quot;

If you select &quot;model&quot;, and enter &quot;Buick Ford&quot;, then it should produce a query like&quot;

SELECT * FROM cars where lower(model) regex buick|ford

______________________________________________________________________
TANSTAAFL!
 
Here are my two cents (which may be the wrong currency for your locale): I just did the same thing but cheated and left a note to the user to enter only one term to get the most results.

What I will eventually do when I have the time (and what you probably need to do as well) is to break up the string wherever there is a space, then use the broken portions in an array.

...But I got hung up on how to pass that array into the query... Does one create several queries or can the array of several search words fit into a single query?
 
I hit submit moments after sleipnir214 posted last - - it seems sleipnir214 has the answer.

Ignore my previous rambling.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top