php - Concatenate All Rows From One Column With MYSQL -
this question has answer here:
- sql, how concatenate results? 7 answers
i trying thought simple it's driving me crazy.
i have following data:
id --- name 1 --- joe 2 --- bob 3 --- jim 4 --- mike
i want able show results of mysql as:
"joe", "bob", "jim", "mike"
i tried concatenate tutorials, seem merging id's.
$sql = "select names, concat_ws('', 'names') namelist peoplenames"; $result = $conn->query($sql); echo $row["namelist"]; if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $names = $row["nameslist"]; echo $names; } }
if echo outside loop recent result.
any ideas?
the problem overwriting contents of $names
each time round loop.
change code this
$sql = "select names peoplenames"; $result = $conn->query($sql); $names = null; if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $names .= sprintf('"%s",',$row['names']); } // output amalgamated date rtrim($names, ','); echo $names; }
Comments
Post a Comment