tagteamcomputing Posted November 8, 2017 Share Posted November 8, 2017 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 Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 8, 2017 Share Posted November 8, 2017 Do you at least get some data from database? Or is it empty too? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.