Jump to content

csv export has no data


tagteamcomputing

Recommended Posts

I am using the following for outputting a csv file and I don't get any errors, however, I also don't get any data

//mysql connection
info here

//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);

//Create our SQL query.
$sql = "SELECT status, orderid, DATE_FORMAT(FROM_UNIXTIME(date), '%m/%d/%Y') as orderdate, email, b_state, s_country, subtotal FROM xcart_orders where date BETWEEN $BeginDate AND $EndDate AND (status ='C' OR status ='P') ";

//Prepare our SQL query.
$statement = $pdo->prepare($sql);

//Executre our SQL query.
$statement->execute();

//Fetch all of the rows from our MySQL table.
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

//Get the column names.
$columnNames = array();
if(!empty($rows)){
    //We only need to loop through the first row of our result
    //in order to collate the column names.
    $firstRow = $rows[0];
    foreach($firstRow as $colName => $val){
 }
}

//Setup the filename that our CSV will have when it is downloaded.
$fileName = 'EmailAddresses-export.csv';

//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');

//Open up a file pointer
$fp = fopen('php://output', 'w');

//Start off by writing the column names to the file.
fputcsv($fp, $columnNames);

//Then, loop through the rows and write them to the CSV file.
foreach ($rows as $row) {
    fputcsv($fp, $row);
}


//Close the file pointer.
fclose($fp);
?>

Thanks for any help

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.