Page 1 of 1

mysql querry

Posted: Fri Sep 05, 2003 3:27 pm
by nincha
any tips on using php to querry a mysql database row base on a keyword? say theres 5 out of 19 rows with keyword "apple" i would like to retrieve those 5 rows with apples.

Posted: Fri Sep 05, 2003 4:43 pm
by qads

Code: Select all

<?php
$search_word = addslashes($_REQUEST[search_word]);
$query = mysql_query("select `field1`, `field2`, `field3` from `table` where `field` = '%$search_word%'");
?>
is this what your looking for?

Posted: Fri Sep 05, 2003 4:53 pm
by JAM
qads wrote:

Code: Select all

<?php
$search_word = addslashes($_REQUEST[search_word]);
$query = mysql_query("select `field1`, `field2`, `field3` from `table` where `field` = '%$search_word%'");
?>
is this what your looking for?
Wouldn't that be;

Code: Select all

<?php
$search_word = addslashes($_REQUEST[search_word]);
$query = mysql_query("select `field1`, `field2`, `field3` from `table` where `field` LIKE '%$search_word%'");
?>
(LIKE)

Posted: Fri Sep 05, 2003 10:02 pm
by brewmiser
I am no expert at this by no means, but I think that both are correct. Qad's will search for the statment "apple" and nothing else. Where as JAM's will search for anthing that has apple in it, i.e. "apple", "applesauce", "appleton", etc. :)

Posted: Sat Sep 06, 2003 5:50 am
by JAM
Just didn't know that you could use % in a caluse with =.

% means anything.
= means exactly.

But combined? Learn something new everyday.

Posted: Wed Sep 24, 2003 6:32 pm
by qads
nincha didn't say if it would be a block of text or just 1 word..so i typed it up, thought if he wants to do wild card search then he would post back or something 8)

Posted: Wed Sep 24, 2003 7:05 pm
by McGruff
See also (no suffixes or prefixes) " .. RLIKE [[:<:]]" . $word . "[[:>:]] .. " and MATCH.

But, if the column only stores a single value (rather than a block of text), "SELECT .. WHERE col='$word'" will do the trick.