1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Insert data from db into csv file with php pdo

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 28, 2024 às 15:43.

  1. Stack

    Stack Membro Participativo

    I'm trying to insert data that I select from my database into a csv file. I got stuck here and don't know why it doesn't work. It keeps me giving a file like this: [​IMG]

    For some reason it put the column names into 1 field (A1) I'm using this code now:

    <?php
    include "includes/connection.php";

    if(isset($_POST['submit'])){

    $tabel = $_POST['tabel'];
    $date1 = $_POST['date1'];
    $date2 = $_POST['date2'];

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename="data.csv";');
    $output = fopen("php://output", "w");
    fputcsv($output, array('Dev_ID', 'Barcode', 'Naam', 'Ip_adres', 'Merk', 'Model', 'CPU', 'Memory', 'Moederbord', 'Serialnummer', 'Aanschaf_dat', 'Aanschaf_waarde', 'Opmerkingen', 'Picture_dev'));
    $sql = "SELECT * FROM ".$tabel." WHERE Aanschaf_dat BETWEEN ".$date1." AND ".$date2."";
    $query = $conn->prepare($sql);
    while($row = $query->fetch(PDO::FETCH_ASSOC))
    {
    fputcsv($output, $row);
    }
    fclose($output);
    }
    ?>


    Does anyone know what I'm doing wrong?

    Continue reading...

Compartilhe esta Página