PHP/MySQL Query String Matching?

Cathy V

New member
In the rows where `City`='Dallas', there is no record where the `Products` field contains the word "restaurant" yet.

My query is "SELECT * FROM tablename WHERE `City`='Dallas' AND( `Products` LIKE '%restaurant%') ORDER BY `ZIP` ASC"

It is returning all the rows where `City`='Dallas' even though the `Products` field does not contain the keyword "restaurant".

What's wrong with the query. How should the query be formulated in order to return only records where the `Products` field contains the keyword "restaurant" and return zero results if that keyword is not found in any record where `City`='Dallas'?

Thank you in advance
 
"SELECT * FROM tablename WHERE `City`='Dallas' AND( `Products` LIKE '%restaurant%') ORDER BY `ZIP` ASC"

alter to

$query = mysql_query("SELECT * FROM tablename WHERE City='Dallas' && Products LIKE %resturant% ORDER BY Zip ASC");

one thing that i see is if you are looking for "restaurant" then there is no need for %restaurant%

you should

$query = mysql_query("SELECT * FROM tablename WHERE City='Dallas' && Product='restaurant' ORDER BY Zip ASC");

if you are allowing someone to type in the type of product then i would suggest you add a function that will correct their spelling before running an insert query.
 
Back
Top