You could use MySQLi Server Behaviors to create a comma separated repeated region for your data. Then just add the necessary headers to have the file downloaded and saved as a CSV like:
header('Content-Type: application/octet-stream');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=yourfileneame.csv');

