mysql querry

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

mysql querry

Post 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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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)
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post 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. :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

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 »

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)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Post Reply