简体   繁体   中英

PHP: Search mysql database using a string?

I have a column (attr) in mysql database with values like this:

   1- apples, red, big, UK,
   2- Green, apples, big, UK, 
    ETC ETC

Its basically a string of words and they can be in any order as you can see above.

I want to search inside that column using any given string. The strings can look like this:

UK, apples, Red, big, 
apples, UK, big, green, 
etc etc

what i need to do is to look into that column and display the results ONLY if the words given in the string matches the exact same words inside that column.

I've tried using this code:

$sql="SELECT * FROM `product_details` WHERE '$list' LIKE CONCAT('%', attr ,'%')";

where the $list is the given strings. like: UK, apples, Red, big,

as you can see, the words exist in the database but they are in different ORDER and that is why the PHP doesn't show any result!

Could someone please advise on this issue?

Try to make an array of this

UK, apples, Red, big, 

prepare condition in PHP and than put this data/words like in that topic:

Is there a combination of "LIKE" and "IN" in SQL?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM