doudaiyao0934 2015-10-05 13:33
浏览 73

PHP:使用字符串搜索mysql数据库?

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?

  • 写回答

1条回答 默认 最新

  • duanjiao7440 2015-10-05 13:39
    关注

    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?

    评论

报告相同问题?