Issue forcing download of CSV File | PHP - php

Essentially I have the following script that should interpret the data from the query, format as CSV and download the CSV file.
While the data and CSV structure is being generated correctly, the file is not downloading. Instead the data is just being presented on the web page:
<?php
//Our MySQL connection details.
$host = '';
$user = '';
$password = '';
$database = '';
//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);
//Create our SQL query.
$sql = " ";
//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){
$columnNames[] = $colName;
}
}
//Setup the filename that our CSV will have when it is downloaded.
$fileName = 'mysql-export.csv';
//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=mysql-export.csv");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header("Content-type: application/force-download");
//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);

The headers should be:
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="mysql-export.csv"');
Note that it's "text/csv", not "application/csv". And you shouldn't need the other two (application/octet-stream and application/force-download). I hope that helps.

Related

get column names when exporting from mysql to csv using PHP

I am trying to export a table from a mysql db to csv.I am able to do this successfully,however, it doesnt export the column names,is there any way i achieve this?Some pointers would be great.Please check out my data_export.php file below:
<?php
include 'class.user.php';
$conn=mysqli_connect("localhost","root","PASSWORD","reflective");
#declare the school variable for use in the select* query
$school = $_POST['school'];
#query the students table for data to export
$select="SELECT * FROM students WHERE school_id ='$school'";
$result = mysqli_query($conn, $select);
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysqli_num_fields($result);
$headers ="";
while ($property = mysqli_fetch_field($result)) {
$headers.= $property->name.",";
}
$headers.="\n";
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="students_data.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
?>
I would suggest you use the array_keys() method in PHP. It will extract all the keys from the array you fetch from the database. You just need to pass the first array as argument in the method like this,
array_keys($data[0]);

PHP: PDO + CSV export not downloading (headers issue?)

I'm having a hard time making my CSV export function work.
I've found around plenty of examples using mysqli_* functions, but I'm actually using PDOs so I had to adapt some questions/answers to my needs.
From what I can see, I'm properly parsing and writing data to the *.csv file, but at the end I don't simply get any "Download" modal from the browser.
Again, looking around, I've understood I may have some kind of problem with my headers, so I'm asking for your help.
This is my PHP function snippet:
function downloadAsCSV($dsn, $dbUser, $dbPsw, $options) {
// New PDO connection.
$pdo = pdoConnect($dsn, $dbUser, $dbPsw, $options);
$query = ... // Not going to write it down.
// Let's query the database, ...
$stmt = $pdo->prepare($query);
// Setting up the query variables here...
[...]
// Executing the statement...
$stmt->execute();
// Headers.
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename='export.csv'");
header("Pragma: no-cache");
header("Expires: 0");
// Let's open tbe "*.csv" file where we'll save the results.
$fp = fopen("php://output", "w");
if ($fp) {
$first_row = $stmt->fetch(PDO::FETCH_ASSOC);
$headers = array_keys($first_row);
fputcsv($fp, array_values($first_row));
fputcsv($fp, $headers);
// ... fetch the results...
$workpiecesArray = $stmt->fetchAll();
// ... and, finally, export them.
foreach ($workpiecesArray as $workpiece) {
fputcsv($fp, array_values($workpiece));
}
}
fclose($fp);
// Closing the connection.
$stmt = null;
$pdo = null;
}
function mysqli_field_name($result, $field_offset) {
$properties = mysqli_fetch_field_direct($result, $field_offset);
return is_object($properties) ? $properties->name : null;
}
I've taken inspiration to write the table column titles from this answer.
What you need to do is to print the file, which you're not doing right now.
See this example using readfile:
http://php.net/manual/en/function.header.php#example-5554
Simply put:
1.Replace
$fp = fopen("php://output", "w");
with
$tmpfname = tempnam("/", "");
$fp = fopen($tmpfname, "w");
2.Replace
fclose($fp);
with
fclose($fp);
readfile($tmpfname);
unlink($tmpfname);
and this will work
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="export.csv"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('export.csv'));
readfile($csvFile);
exit;
`put this after` fclose($fp);

Adding the headers when creating a csv file PhP, SQL

I have the following code:
PhP
function write_csv($filename, $rows) {
$file = fopen($filename, 'w');
while($row = mysqli_fetch_assoc($rows)) :
fputcsv($file, $row);
endwhile;
fclose($file);
}
// $db is a class I have and this function just returns a SQL result set
$result_set = $db->run_query("SELECT * FROM Users");
write_csv('../MOCK_USERS_DATA.csv', $result_set);
I am correctly getting a created CSV file with the the users information for example
1,greg,mason,407-356-3322,greg#gmail.com
2,derek,herd,407-234-4352,derek#gmail.com
etc...
My question is how do I get the headers from the table as the first row in my CSV file. I am new to creating CSV files and am stuck on getting that first row of headers in that file that match the name of the column in my database. Any suggestions?
Easy export to CSV
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
ob_start();
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings -> trim is used for lang translates
// or use SQL query to get table columns headers name
fputcsv($output, array(
trim('Product name'),
trim('Pellet number'),
trim('Quantity'),
trim('Date')
), ";");
// use foreach
***********
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header('Content-Disposition: attachment; filename=pelletlist-' . time() . '.csv');
header("Content-Transfer-Encoding: binary");
header("Cache-control: private"); //use this to open files directly
header('Expires: 0');
header("Pragma: no-cache");
header("Connection: close");
fclose($output);
exit();
for load column name from db use:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
or
SHOW COLUMNS FROM my_table;

I want to save table in a cell of CSV but changing line automatically in case of large table

![I want to save table in a cell of CSV but changing line automatically in case of large table
thanks in advance
code to write CSV`
when I export csv of scraped data it messed up when table contains large number of rows or tr's please help me out of this.
`
$fp = fopen('stream.csv', "w");
foreach ($output as $row) {
fputcsv($fp, $row);
}
fclose($df);
fclose($fp);
Question is really not clear enough but here is how I generate my CSV files. Good thing is that it also shows column names automatically.
$result = mysqli_query ($mysqliConn,"SELECT * FROM core ORDER BY start_datetime ASC");
$fields = mysqli_fetch_fields($result);
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysqli_fetch_fields($result);
$headers = array();
foreach ($fields as $field) {
$headers[] = $field->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="core.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
Hope this helps.

Generating excel sheet from MySql database using PHP

I'm new to PHP. I'm using the following code to generate an excel sheet from Mysql database using PHP.
<?php
include("../Connection/Connection.php");
$db_con=new Connection();
$db_con->get_connection(); //Opens a database connection. This function is contained in the Connection.php which is included above.
$result = mysql_query("SELECT * FROM country");
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_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 = mysql_fetch_array($result))
{
fputcsv($fp, array_values($row));
}
die;
}
?>
The above code is working and it generates the specified file export.csv but the problem is that it generates each column from MySql in this file twice. In other words, each column is duplicated twice (headers displayed are not duplicated though). What is wrong with this code? What changes should be made so that it displays each column exactly once?
Use mysql_fetch_row instead.
mysql_fetch_array fetches an array with BOTH string indexes and numeric indexes, so each value is fetched twice. Your code would work with either mysql_fetch_assoc or mysql_fetch_row.

Categories