Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP导出csv文件以从mysql下载_Php_Mysql - Fatal编程技术网

使用PHP导出csv文件以从mysql下载

使用PHP导出csv文件以从mysql下载,php,mysql,Php,Mysql,我正在尝试从mysql数据库获取csv文件,并将其发送到浏览器进行下载 我读了一些关于它的帖子,但我无法下载文件 当我运行代码时,文件不会被下载 我通过ajax向该文件发送帖子 这是我的密码: <?php download_send_headers(); $mysqli = new mysqli(*****); $mysqli->set_charset("utf8"); $sql = "SELECT trainer, COUNT(trainer

我正在尝试从mysql数据库获取csv文件,并将其发送到浏览器进行下载

我读了一些关于它的帖子,但我无法下载文件

当我运行代码时,文件不会被下载

我通过ajax向该文件发送帖子

这是我的密码:

<?php

    download_send_headers();

    $mysqli = new mysqli(*****);

    $mysqli->set_charset("utf8");

    $sql = "SELECT trainer, COUNT(trainer) FROM personal_traning WHERE gym = ? GROUP BY trainer"; 

    $stmt = $mysqli->prepare($sql);

    $stmt->bind_param('s', $_POST['gym']);

    $stmt->execute();

    $stmt->store_result();

    $stmt->bind_result($trainer, $trainerCount);

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

    $results = array();

    $row = array();

    $row[0] = "Trainer name";
    $row[1] = "Count";

    fputcsv($f, $row); 

    while ($stmt->fetch()) 
    {

    $row = array();

    $row[0] = $trainer;
    $row[1] = $trainerCount;

    fputcsv($f, $row); 

    }

    $output = stream_get_contents($f);

    fclose($f);

    echo $output;

    die();

function download_send_headers() {

    header('Content-Disposition: attachement;filename="name";');

    header('Content-Type: application/csv; charset=UTF-8');
}


?>

尝试将.csv添加到标题的文件名中