SQL LIKE % inside array

后端 未结 8 706
我寻月下人不归
我寻月下人不归 2020-12-05 04:52

I know how to perform an SQL LIKE % query for a single value like so:

SELECT * FROM users WHERE name LIKE %tom%;

but how do I do this if th

8条回答
  •  执念已碎
    2020-12-05 05:46

    i just took the code of 472084.

    $sql = array('0'); // Stop errors when $words is empty
    
    foreach($words as $word){
        $sql[] = 'name LIKE %'.$word.'%'
    }
    
    $sql = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);
    

    For my self, i had to modify it because it's returned me an error SQL. I Post it for people who gonna read the thread.

    foreach($words as $word){
        $sql[] = 'name LIKE \'%'.$word.'%\'';
    }
    
    $sql = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);
    

    The difference between them is about quote, my mysql DB said there is a problem ! so i had to escape quote from $sql[] = 'name LIKE %'.$word.'%' and now it's work perfectly.

提交回复
热议问题