prepare($query); $sth->execute(); $patterns = array(); while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { $patterns[] = $row['name']; } $msg = ""; foreach ( $patterns as $pattern ) { $msg .= $pattern . ","; } $msg = substr($msg, 0, -1); echo $msg;