0

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

How can I read the list and run a PHP script to find if the user that is logged in (that is stored in the Session) is in that list. For example, how could I see if the logged on user (MTNOfficial) is in the list. I think it's a PHP if statement with a MySQL CONCAT or FIELD_IN_SET or something.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

Use FIND_IN_SET() to find the records that contain your user

select *
from your_table
where find_in_set('MTNOfficial', usersBought) > 0
juergen d
  • 201,996
  • 37
  • 293
  • 362
0

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!
        }
    }
}
Esaevian
  • 1,707
  • 5
  • 18
  • 30