You can concatenate your tags and comma at each iteration
$tags .= $row['tags'] . ',';
To remove the last comma you just need substr()
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// concatenate all the tags into one string
$tags .= $row['tags'].',';
}
$tags = substr($tags,0,-1);