Find specific value in comma list in database

前端 未结 2 459
傲寒
傲寒 2021-01-28 20:04

In one of my tables, i have a column where it has a comma seperated list of users that have bought that item. IMAGE

How can i read the list and run a php script to find

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 20:56

    Get the comma separated list, then try the php function explode() to get an array that you can loop over.

    http://php.net/manual/en/function.explode.php

    $target = "MTNOfficial";
    $query = "select usersBought from table_name";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $users = explode("," $row['usersBought']);
        // may want to trim() here
        for ($i = 0; $i < count($users); i++) {
            if ($users[i] == $target) {
                //we found him!
            }
        }
    }
    

提交回复
热议问题