Forum Moderators: coopster
Of course this doesn't work if you have fruits that have name of another fruit in the name:
SELECT * FROM table WHERE fruits LIKE '%grape%'
This query will find rows with 'grape' and rows with 'grapefruit'.
More info on pattern matching can be found from MySQL Reference Manual:
[dev.mysql.com...]
function searchFruit($fruit) {
$sql = "SELECT * FROM table WHERE fruits REGEXP '^".$fruit."$'";
// Rest of the code
}
[edited by: deMorte at 11:05 am (utc) on Oct. 14, 2008]
notice the use of blank spaces, this will help solve the problem deMorte has mentioned, i.e. it will not return grapefruit for grape.
2) Extract that value in a variable
3) split() the Variable into an array using whatever delimeter you have in your field, i think whitespace?
4) run in_array() on that array and that will return true or false for your fruit name
Infact, i think you only needed that Query, rest is not needed i think but i dont like deleting what i have already posted. That Query itself will return only those rows which have exact match on your fruit name
[edited by: Anyango at 3:43 pm (utc) on Oct. 14, 2008]
The second question: If you are looking for a precise name then any forms of % will bring the exact match plus variations
if you know what you are looking for you will need a subquery such as
WHERE
fruit='$fruit'
$fruit is the one fruit you are looking for