Selecting results as a comma-separated string The below code is working correctly, however, I am not sure if the combination of while, foreach and implode is really necessary here or if the same can be achieved much simpler.

What I do here is fetching some data (text) from a db and the only thing I need is the results as a comma separated string.

Now even though this is working I was wondering if there is a direct way to get the results as a comma separated string since the arrays here are not needed for anything else.

$stmt = $conn-&gt;prepare("SELECT " . $languageFrm . " FROM TranslationsMain WHERE location LIKE ? ORDER BY sortOrder, " . $languageFrm);
$stmt-&gt;bind_param("s", $location);
$stmt-&gt;execute();
$result = $stmt-&gt;get_result();
$strTranslations = array();
while($arrTranslations = $result-&gt;fetch_assoc()){
    $translations[] = $arrTranslations;
}
foreach($translations as $translation){
    array_push($strTranslations, $translation[$languageFrm]);
}
echo implode(",", $strTranslations);
Using one of the below queries removes the need to iterate over the result sets to construct the CSV strings. When a single result is expected, a -&gt;fetch() instead of -&gt;fetchAll() would do.

Conclusion: implode goes away, while and for loops go away the only iteration needed would be in case of multiple CSV result sets.

SELECT GROUP_CONCAT(columnName) FROM Table1; # for single column

SELECT CONCAT(columnA,",",columnB) from Table2; # for multi cols


You might have issues creating prepared statements for this; I don't know.

example for the first query:

&lt;?php

$conn = new PDO("mysql:host=localhost;dbname=test", "root", "");

$stmt = $conn-&gt;query("SELECT GROUP_CONCAT(columnName) FROM Table1");

$result = $stmt-&gt;fetch(PDO::FETCH_ASSOC);

var_dump($result);