I want to separate the values that come out of the DB by a comma. I can't find an SQL way to do it, and if I try to do it in the while loop it puts a comma on the end of the string, which I do not want.
private function retrieve_subjects(){
$dbh = $this->connect();
$stmt = $dbh->prepare("SELECT name FROM subject");
$stmt->execute();
$stmt->bindColumn('name', $name);
while($stmt->fetch(PDO::FETCH_BOUND)){
$sub .= $name;
}
return $sub;
}
Give this a try:
$sub = '';
while($stmt->fetch(PDO::FETCH_BOUND)){
if($sub == '')
$sub = $name;
else
$sub .= ',' . $name;
}
Try using implode
.
PHP: http://php.net/manual/en/function.implode.php
MySQL (GROUP_CONCAT): http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat
Use a delimiter variable
$delim = "";
while($stmt->fetch(PDO::FETCH_BOUND)){
$sub .= $delim;
$sub .= $name;
$delim = ", ";
}