0

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.

5
  • I don't know what this has to do with concatenation. Do you want to concatenate the results? Something like GROUP_CONCAT()? Commented Sep 23, 2023 at 9:47
  • I would check the values the get in $row as well. Commented Sep 23, 2023 at 9:49
  • Make sure you have PHP error reporting enabled on full. You should be getting a Warning due to the bug you have. If you do not see a warning, it means you have error reporting disabled. Commented Sep 23, 2023 at 9:52
  • Hi, as I wrote I need to concatenate distinct values obtained in a single PHP variables. Commented Sep 23, 2023 at 9:54
  • Nigel, IF I echo the $row, it tells me: ArrayArray Commented Sep 23, 2023 at 9:56

1 Answer 1

1
$suppliers = '';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
  if($suppliers !== '') $suppliers .= '-';
  $suppliers .= $row['COLUMN'];
}
echo $suppliers; 
Sign up to request clarification or add additional context in comments.

2 Comments

Found the problem ! It works now. Great answer Aysun!
i am happy it worked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.