简体   繁体   中英

Export a CSV File for a 'username' in PHP

I have data in a MySQL database. I am sending the participant a URL to get their data out as a CSV file. How can I, when they click the link, ... A download link is there to download all data from the database in csv format.

<?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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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