sql >> Database >  >> RDS >> Mysql

Tabel exporteren naar CSV via php-knop

Het is 2017 en mysqli komt nu vaker voor dan mysql. Dus hier is een mysqli-versie van het antwoord van Josh Liptzin:

<?php

/* Attempt MySQL server connection. */  
$connection = mysqli_connect($database_server, $database_username, $database_password, $database_name);

// Check connection
if($connection === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$query = "SELECT * FROM Employee_data";
$result = mysqli_query($connection, $query);

$number_of_fields = mysqli_num_fields($result);
$headers = array();
for ($i = 0; $i < $number_of_fields; $i++) {
    $headers[] = mysqli_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="export.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
        fputcsv($fp, array_values($row));
    }
    die;
}

function mysqli_field_name($result, $field_offset)
{
    $properties = mysqli_fetch_field_direct($result, $field_offset);
    return is_object($properties) ? $properties->name : null;
}

?>


  1. Wat zijn de gegevens en informatie en gegevensvolumes?

  2. System.Data.OracleClient vereist Oracle-clientsoftware versie 8.1.7 of hoger

  3. Herstel uw WordPress-database met WP-CLI

  4. RAWTONHEX() Functie in Oracle