Export Data to CSV File using PHP and MySQL




<?php 

// Load the database configuration file
include_once 'dbConfig.php';

// Fetch records from database
$query = $db->query("SELECT * FROM members ORDER BY id ASC");

if(
$query->num_rows > 0){
    
$delimiter = ",";
    
$filename = "members-data_" . date('Y-m-d') . ".csv";
    
    
// Create a file pointer
    
$f = fopen('php://memory', 'w');
    
    
// Set column headers
    
$fields = array('ID', 'FIRST NAME', 'LAST NAME', 'EMAIL','STATUS');
    
fputcsv($f, $fields, $delimiter);
    
    
// Output each row of the data, format line as csv and write to file pointer
    
while($row = $query->fetch_assoc()){
        
$status = ($row['status'] == 1)?'Active':'Inactive';
        
$lineData = array($row['id'], $row['first_name'], $row['last_name'], $row['email'], $status);
        
fputcsv($f, $lineData, $delimiter);
    }
    
    
// Move back to beginning of file
    
fseek($f, 0);
    
    
// Set headers to download file rather than displayed
    
header('Content-Type: text/csv');
    
header('Content-Disposition: attachment; filename="' . $filename . '";');
    
    
//output all remaining data on a file pointer
    
fpassthru($f);
}
exit;

?>

Post a Comment

0 Comments