Search code examples
phpmysqlvariablesconcatenationdistinct

How to put result of MYSQL DISTINCT QUERY in a PHP VARIABLE?


i've a problem trying to get multiple values and concatenate them inside a single php string variable.

The idea is to use a MYSQL DISTINCT STATEMENT on a single column of the table like this:

$sql = "select DISTINCT(COLUMN) from table where <multiple conditions>";
$result = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$suppliers = $row['COLUMN'];
}

I'm sure there are 2 records in my database I want to obtain with two distinct values: 'PLENITUDE' and 'IREN' but i dont know how to concatenate them inside the variable like this:

'PLENITUDE-IREN'

And i'm not so expert .. it seems the query is OK but if I echo $suppliers the result is null.


Solution

  • $suppliers = '';
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      if($suppliers !== '') $suppliers .= '-';
      $suppliers .= $row['COLUMN'];
    }
    echo $suppliers;