Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
nincha
Forum Contributor
Posts: 191 Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA
Post
by nincha » Fri Sep 05, 2003 3:27 pm
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.
qads
DevNet Resident
Posts: 1199 Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane
Post
by qads » Fri Sep 05, 2003 4:43 pm
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?
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Fri Sep 05, 2003 4:53 pm
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)
brewmiser
Forum Commoner
Posts: 74 Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS
Post
by brewmiser » Fri Sep 05, 2003 10:02 pm
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.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Sep 06, 2003 5:50 am
Just didn't know that you could use % in a caluse with =.
% means anything.
= means exactly.
But combined? Learn something new everyday.
qads
DevNet Resident
Posts: 1199 Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane
Post
by qads » Wed Sep 24, 2003 6:32 pm
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
McGruff
DevNet Master
Posts: 2893 Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland
Post
by McGruff » Wed Sep 24, 2003 7:05 pm
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.