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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search in DB Problem

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
Hi there,

I have a problem regarding searching for keywords from a field in a table in MySQL.

In a.php page:

<form method=&quot;post&quot; action=&quot;b.php&quot;>
<input name=&quot;Keywords&quot;>
</form>

In b.php page:

$Query = &quot; SELECT * FROM TableName WHERE FieldName like '%$_POST[Keywords]%' &quot;;

This will works only if the user wrote 1 word and that word is exactly in that field, BUT what I want is:

to add another inputs to the form:

<input type=&quot;radio&quot; name=&quot;SearchMethod&quot; value=&quot;AllWords&quot;>
<input type=&quot;radio&quot; name=&quot;SearchMethod&quot; value=&quot;AnyWord&quot;>

So if the user choused &quot;AllWords&quot;, the query should returns the records that only have all words and not nesesary same order and not same letter case. (i.e. the user searched for &quot;Toyota Supra&quot; and in DB is &quot;supra toyota&quot;)

And if he choused &quot;AnyWord&quot;, how can I test each word in the &quot;Keywords&quot; input?

I hope I was clear :rolleyes:
 
Something like this perhaps?

[tt]$query = &quot;SELECT * FROM table WHERE &quot;;
$separator = (($_GET['SearchMethod'] == &quot;AllWords&quot;) ? &quot; AND &quot; : &quot; OR &quot;;
$words = explode(&quot; &quot;, $_GET['q']);
$query .= &quot;field LIKE '%&quot; . $words[0] . &quot;%'&quot;;
for ($i = 1; $i < count($words); $i++)
$query .= $separator . &quot;field LIKE '%&quot; . $words[$i] . &quot;%'&quot;;[/tt] //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top