在wordpress中导出到csv并下载

时间:2016-06-11 10:10:10

标签: php mysql wordpress csv export

我在数据库中有一个表,我想通过单击导出到csv按钮将其导出到csv。我正在使用wordpress,我的数据库中有一个自定义表

if(isset($_POST['btnMonth'])){
    $month = $_POST['hidMonth'];
    $userId = $_POST['hidUserId'];
    //$path = testing($month, $userId);

    $conn = mysql_connect('localhost', 'sensible_bps', 'inception1111');
    mysql_select_db('sensible_bps', $conn);

    $filename = "toy_csv.csv";
    $fp = fopen('php://output', 'w');

    if($month == 'All'){
        $query= "SELECT * FROM clientsdata WHERE Client_Id = '".$userId."'";
    }else{
        $query= "SELECT * FROM clientsdata WHERE Client_Id = '".$userId."' and Month = '".$month."'";
    }

    $result = mysql_query($query);
    while ($row = mysql_fetch_row($result)) {
        $header[] = $row[0];
    }   

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);

    fputcsv($fp, $header);

    $query= "SELECT * FROM clientsdata WHERE Client_Id = '".$userId."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_row($result)) {
        fputcsv($fp, $row);
    }
    exit;

}

但是这段代码工作不正常请帮助解决这个问题 感谢名单

0 个答案:

没有答案