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!

MySQL search problems

Status
Not open for further replies.

bentleykf

Programmer
Apr 29, 2002
59
AU
I've created a MySQL search engine in php (a very simple one) and I want it to search for words inside a feild rather than search the entire feild. The search engine I have now only accomodates for exact phrase matching (eg. when you search for "anna friel" it returns the record with the name value equal to "anna friel" [case-insensitive], but when you search for "anna" it returns no records). This is acceptable until names like "adriana sklenarikova" come along, which is a pretty hard name to remember off by heart, especially for my intended market.

What I want to create is a search engine that searches through search scripts in a MySQL feild which are seperated by commas. For example, lets say that I have a MySQL table with a feild called "search". In that feild is a series of search strings seperated by commas (eg. "adriana,sklenarikova,adriana sklenarikova,adriana karembeau,karembaeu,etc..."). I want the search engine to take a search query of "adriana" and return all the records with the search string "adriana" inside the search feild of the MySQL table. Is this even possible?

My MySQL search script
-------------------------------------------
<html><title>Search</title><body>
<?php
$db = mysql_connect(&quot;localhost&quot;, &quot;username&quot;,&quot;password&quot;) or die (&quot;Could not connect to server&quot;);
@mysql_select_db(&quot;planetmirror_db&quot;,$db) or die (&quot;Could not connect to database&quot;);
$query = &quot;SELECT * FROM celebdb WHERE name LIKE '$search' &quot;;
$result = mysql_query($query,$db) or die (&quot;Could not connect to table&quot;);

if ($myrow = mysql_fetch_array($result)) {
printf(&quot;<b>Name:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;name&quot;));
printf(&quot;<b>DOB:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;dob&quot;));
printf(&quot;<b>Feild:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;feild&quot;));
printf(&quot;<b>Description:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;desc&quot;));
printf(&quot;<b>Pic Location:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;picloc&quot;));
printf(&quot;<b>File Location:</b> %s<br>\n&quot;, mysql_result($result,0,&quot;fileloc&quot;));
}
else {
printf(&quot;Sorry, no records found\n&quot;);
}
?>
</body></html>
-----------------------------------------
Note: The variable $search is defined previously by a form which puts the variable in the url.
 
yes it is

try this

select * from users where search like '%,$search,%' or search like '$search,' or search like '%,$search'

this query will return all the rows that the field search have the $search in the front, middle or end of the field.

I gave 3 or conditions to avoid the string anna to return if you have the field something like

'anna bukeroft,bukeroft'

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top