分离价值观的最佳方式

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;
}

Use a delimiter variable

$delim = "";
while($stmt->fetch(PDO::FETCH_BOUND)){
    $sub .= $delim;
    $sub .= $name;
    $delim = ", ";
}