Php 下载csv,但带逗号的行为空

Php 下载csv,但带逗号的行为空,php,mysql,Php,Mysql,伙计们,我得到的代码确实有效,但数据库中有些行的值是逗号,这些行被下载为空。这有什么办法 这是我的php <?php require_once('config.php'); $y = $_REQUEST['y']; $m = $_REQUEST['m']; $date = "$y-$m"; header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename=Data-Backup-' .

伙计们,我得到的代码确实有效,但数据库中有些行的值是逗号,这些行被下载为空。这有什么办法

这是我的php

<?php

require_once('config.php');

$y = $_REQUEST['y'];
$m = $_REQUEST['m'];
$date = "$y-$m";
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=Data-Backup-' . $date . '.csv');
$select_table = mysql_query("SELECT * FROM records WHERE DATE_FORMAT(data_submitted, '%Y-%m') = '$date'  ORDER BY ID DESC");
$rows = mysql_fetch_assoc($select_table);

if ($rows) {
    getcsv(array_keys($rows));
}

while ($rows) {
    getcsv($rows);
    $rows = mysql_fetch_assoc($select_table);
}

function getcsv($no_of_field_names)
{
    $separate = '';
    foreach ($no_of_field_names as $field_name) {
        if (preg_match('/\\r|\\n|,|"/', $field_name)) {
            $field_name = '' . str_replace('', $field_name) . '';
        }
        echo $separate . $field_name;
        $separate = ',';
    }
    echo "\r\n";
}

?>

您可以使用
fputcsv

$output = fopen('php://output', 'w');

$count = 0;
while($row = mysql_fetch_assoc($select_table)) {
    if ($count == 0) {
        // header
        fputcsv($output, array_keys($row));
    }

    fputcsv($output, array_values($row));

    $count++;
}

fpassthru($output);

为什么不改用
fputcsv
呢?怎么用?你能帮我解决这个问题吗?如果从数据库中只下载1条记录,将不胜感激