简体   繁体   English

为 PHP 中的“用户名”导出 CSV 文件

[英]Export a CSV File for a 'username' in PHP

I have data in a MySQL database.我在 MySQL 数据库中有数据。 I am sending the participant a URL to get their data out as a CSV file.我正在向参与者发送一个 URL,以将他们的数据作为 CSV 文件导出。 How can I, when they click the link, ... A download link is there to download all data from the database in csv format.我怎么能,当他们点击链接时,...一个下载链接可以从数据库中以 csv 格式下载所有数据。

<?php
$insquery = "SELECT username FROM r_institution where status=1";
    $exportstmt = $conn->query($insquery);
    $insresults = mysqli_fetch_assoc($exportstmt);
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $content = array();
    $title = array("username");
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $output = fopen('php://output', 'w');
    fputcsv($output, $title);
    foreach ($content as $con) {
        fputcsv($output, $con);
    }
?>
$filename = "testing-exports.csv";

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");

    $insquery = "SELECT username FROM r_institution where status=1";
    //$exportstmt = $conn->query($insquery);
    $insresults = $conn->query($insquery);
    //$insresults = mysqli_fetch_assoc($exportstmt);
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $content = array();
    $title = array("Institution Emails");
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $output = fopen('php://output', 'w');
    fputcsv($output, $title);
    foreach ($content as $con) {
        fputcsv($output, $con);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM