MySQLi WHERE LIKE multiple criteria

前端 未结 4 1957
暖寄归人
暖寄归人 2021-01-13 10:01

I have the following example table and attributes:

---------------------------
|  Name  |       Town     |
---------------------------
| Name 1 |      POOLE          


        
4条回答
  •  抹茶落季
    2021-01-13 10:28

    You could explode the towns, then loop through them and build the query like so:

    $towns = explode('/', $global->getPlayerTown());
    $first = true;
    $like_sql = '';
    foreach($towns as $town) {
        $like_sql .= $first ? ' WHERE ' : ' OR ';
        $like_sql .= "`Town` LIKE '%{$town}%'";
        $first = false;
    }
    $query = "SELECT * FROM `table` {$like_sql}";
    

    However I would recommend you normalise your data, and have a separate towns table, with a user_town pivot table.

提交回复
热议问题