sql data export script, creating corrupt file - php

Ok, so I have the following script that will export data from my database using php.
Everything works fine, except when I try to open the file in excel, I get "file is corrupt".
I have used this script before, and I cannot figure out why it's not working. It may be a simple solution that I cannot see. Thanks in advance!
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM trans",$link);
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM trans",$link);
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";
}
$csv_output .= "\n";
}
$filename = date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;

I think the Content-type is incorrect. Try using this instead:
header("Content-type: text/csv");

I have a few suggestions here:
Have you considered using SELECT INTO OUTFILE? If possible, this is probably the best way to do it.
If you don't want to / can't use SELECT INTO OUTFILE, PHP has native functions for writing CSV output, that handles things like escaping for you. See the fputcsv() manual page for more info. There's some examples in the user comments showing how to output it directly.
Make sure you choose the correct delimeters, escape, etc. when opening up the file in excel

Related

MySQLi Query To Excel Generating Two Blank Row

I am trying to generate excel file for my dynamic query using below codes.
$cname = $_POST["cname"];
$cnote = $_POST["cnote"];
$ccondition = $_POST['sql'];
$crange = $_POST['start'] . "-" . $_POST['end'];
$sql_query = $ccondition;
$filename = "export".date('Ymd') . ".xls";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"$filename\"");
$show_coloumn = false;
$resultset = mysqli_query($mysqli, $sql_query) or die("database error:". mysqli_error($mysqli));
$rowcount=mysqli_num_rows($resultset);
$developer_records = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$developer_records[] = $rows;
}
if(!empty($developer_records)) {
foreach($developer_records as $record) {
if(!$show_coloumn) {
echo implode("\t", array_keys($record)) . "\n";
$show_coloumn = true;
}
echo implode("\t", array_values($record)) . "\n";
}
}
I am facing two issue in it.
1) When I try to open it in excel 2016, its giving error like below but when I press yes its opening file.
2) each my exported excel file have two blank row on top before header row, I don't know from where its coming and how I can fix it.
I have searched lot for it but I have not found any working solution for it. Let me know if someone can help me for solve the issues. Thanks

PHP not creating downloadable CSV file

I am trying to pull out data from my database using php and exporting it into a downloadable CSV file that can be opened with excel. I am able to do this when i use mysql however, many have advised to not include mysql syntax in my code as its being deprecated and instead i should use mysqli. I have changed my code but now my code is not working. Does anyone know why that is?
mysql version (working version)`
mysql_connect('localhost', 'xxxxx', 'xxxxx') or die('connect');
mysql_select_db('db') or die('select');
$result = mysql_query('SELECT * bodyshops_master_network') or die('query');
if(mysql_num_rows($result) == 0)
{
die('no data');
}
$fh = tmpfile() or die('tmpfile');
$cols = array_keys(mysql_fetch_assoc($result));
fputcsv($fh, $cols);
mysql_data_seek($result, 0); // set result row pointer back to first row
while($row = mysql_fetch_assoc($result))
{
fputcsv($fh, $row);
}
rewind($fh);
$text = fread($fh, 999999);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="download.csv"');
header('Content-Length: ' . strlen($text));
echo $text;
exit;
mysqli version (not working):
$mysqli = new mysqli("localhost", "xxxxx", "xxxxx", "db");
if (mysqli_connect_errno())
{
printf("Connect failed: ", mysqli_connect_error());
exit();
} else
{
$result = "SELECT * FROM bodyshops_master_network";
if(mysqli_num_rows($result) == 0)
{
die('no data');
}
$fh = tmpfile() or die('tmpfile');
$cols = array_keys($result->fetch_assoc());
fputcsv($fh, $cols);
$result->data_seek(0); // set result row pointer back to first row
while($row = $result->fetch_assoc())
{
fputcsv($fh, $row);
}
rewind($fh);
$text = fread($fh, 999999);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="download.csv"');
header('Content-Length: ' . strlen($text));
echo $text;
exit;
Check phpinfo to see that mysqli extension is enabled.
Remove/comment the header calls so that you receive the output as plain HTML so that you notice if any message shows up (due to die or coding error) or if you actually get the data.
Also note that you loose the date of the first record you retrieve because you call:
$cols = array_keys(mysql_fetch_assoc($result));
respectively
$cols = array_keys($result->fetch_assoc());
What is not working?
Are you getting any errors?
Is the file empty, is there any file downloading?
Maybe errors aren't enabled, try this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
I'm new on StackOverflow, I think this would help. (I speak spanish, I hope you to understand my english :D )
I've been looking for a simply way to use mysqli and download a csv file that could be read by excel without UTF-8 problems (using ñ,á,ü...). I didn't found it, so I created one by myself (learning from Google and StackOverflow answers), after some hours I got something that works finally.
This is a Class that connects with the database and the functions will do whatever you want using mysqli and PHP. In this case, calling this class (require or include), just use the "downloadCsv()" function.
As an example, this would be the "class.php" file:
<?php
class DB{
private $con;
//this constructor connects with the database
public function __construct(){
$this->con = new mysqli("Your_Host","Your_User","Your_Pass","Your_DatabaseName");
if($this->con->connect_errno > 0){
die('There was a problem [' . $con->connect_error . ']');
}
}
//create the function that will download a csv file from a mysqli query
public function downloadCsv(){
$count = 0;
$header = "";
$data = "";
//query
$result = $this->con->query("SELECT * FROM Your_TableName");
//count fields
$count = $result->field_count;
//columns names
$names = $result->fetch_fields();
//put column names into header
foreach($names as $value) {
$header .= $value->name.";";
}
}
//put rows from your query
while($row = $result->fetch_row()) {
$line = '';
foreach($row as $value) {
if(!isset($value) || $value == "") {
$value = ";"; //in this case, ";" separates columns
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . ";"; //if you change the separator before, change this ";" too
}
$line .= $value;
} //end foreach
$data .= trim($line)."\n";
} //end while
//avoiding problems with data that includes "\r"
$data = str_replace("\r", "", $data);
//if empty query
if ($data == "") {
$data = "\nno matching records found\n";
}
$count = $result->field_count;
//Download csv file
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=FILENAME.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data."\n";
}
?>
After creating the "class.php" file, in this example, use that function on "download.php" file:
<?php
//call the "class.php" file
require_once 'class.php';
//instantiate DB class
$export = new DB();
//call function
$export->downloadCsv();
?>
After download, open the file with MS Excel.
I hope this help you, I think I wrote it well, I didn't feel comfortable with the text and code field.

Export Mysql Data into MS Excel Using a Link\Button in PHP

I am a newbie to PHP and need help in exporting a selected content from Mysql Table to MS Excel using PHP. I need this done by a click of a button or a link.
Below is a piece of code I have done so far but I am consistently getting a warning as "Cannot modify header information - headers already sent". Also suggest a good way to export table on a click of a button\link. Thanks
//Export Contents
$header = '';
$data = '';
$fields = mysql_num_fields($sql);
//fetch header
for($i=0; $i < $fields; $i++)
{
$header .= mysql_field_name($sql, $i)."\t";
}
//fetch data each row, store on tabular row data
while($row = mysql_fetch_row($sql))
{
$line = '';
foreach($row as $value)
{
if(!isset($value) || $value == "")
{
$value = "\t";
}
else
{
$value = str_replace('"', '""', $value);
$value = '"'.$value.'"'."\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
$data = str_replace("\r", "", $data);
}
//Naming the excel sheet
$name = $customerFilter."_".date('d-m-y').".xls";
header("Content-type:application/vnd.ms-excel;name='excel'");
header("Content-Disposition: attachment; filename=$name");
header("Pragma: no-cache");
header("Expires: 0");
//Output Data
echo $header."\n\n".$data;
The mysql query
SELECT <column list>
FROM <table>
INTO OUTFILE '/tmp/somefilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Then redirect to the file
if you got a "headers already sent" you have some output (whitespaces) before sending your excel headers. Please enshure that the <?php tag is the very first in your file.
Please read this thread: How to fix "Headers already sent" error in PHP
You should use a library like this http://phpexcel.codeplex.com/ to be able to output stable excel files.
Here is a hello world example for phpexcel:
http://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home

postgresql query result to csv prints current page's html code also

I am trying to output the results of a PostgreSQL query to CSV format using PHP.
On the main page is a link that sends the SQL statements as a string to another function in another PHP class, which in turn takes the SQL and executes the query using pg_query() and return the result sets.
My problem is that when I open the CSV file, all the results of my query are there, but at the end of the file I see the HTML code from the page that sent the query.
I looked at several StackOverflow posts, but to no avail.
Here is my code:
Main class:
$o .= '<p>You can convert the result set to CSV format to be opened in Excel.</p>';
$link = array('op1' => 'PatternExport', 'op2' => 'outputToCSV', 'id' => $pattern_id, 'data' => $pattern_SQL);
$o .= 'Download Query Results as CSV File';
Receiving class:
function outputToCSV()
{
$ptid = $this->oQS->getValue('id');
$sql = $this->oQS->getValue('data');
$href = $this->oQS->buildEncryptedURL(array('op1'=>'PatternManager', 'op2'=>'listPatterns'),'/aatsc/index.php');
$result = pg_query($sql);
// filename for download
$filename = "query_results_" . date('Ymd') . "_" . $ptid . ".csv";
$output = fopen('php://temp/maxmemory:' . (12*1024*1024), 'rw+');
foreach(pg_fetch_assoc($result,0) AS $field=>$value)
{
$output .= '' . $field . ',';
}
$output = rtrim($output,',') . "\n";
for($i=0;$i<pg_num_rows($result);$i++)
{
foreach(pg_fetch_assoc($result,$i) AS $field=>$value)
$output .= '' . $value . ',';
$output = rtrim($output,',') . "\n";
}
header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition: attachment; filename=\"$filename\";");
header("Pragma: no-cache");
print($output);
//$href = $this->oQS->buildEncryptedURL(array('op1'=>'PatternManager', 'op2'=>'listPatterns'),'/aatsc/index.php');
//header("Location: $href");
}
Could you tell me whether I am using the right approach to export query results to CSV, and what in my code is causing the whole HTML code to be streamed?
Thanks
You're generating $output twice, if you remove the foreach loop & fopen line, it should work.
You merely just need to issue the SQL COPY statement as follows:
COPY (select * from tbl) to stdout with csv header
resulting in:
col1,col2,col3
2013-05-22 07:28:59.732,192.168.1.67,3

html table to CSV or Excel

Alright, I'm starting to go a little crazy...
jQuery Table to CSV export
I'm looking at that thread.
It does work, but the output does everything by each LINE not by the HTML table rows. For example, I have an address that is in one HTML cell:
1234 Berryman Lane
Atlanta, GA 12345
Unit # 54A
That will be THREE rows when it outputs to excel, instead of one cell, with returns in it.
Further, there's no way to strip out the HTML that is inside of the HTML cells with that solution, as far as I know...
Finally, Excel gives a warning when opening that file.
What I'm getting at, is that I'd rather just have something that can take the inner most data in the HTML cells (not including HTML inside of a cell), and rip it to CSV. Is there anything that does this well these days?
UPDATE
Well, I just found the best thing yet, this is pretty perfect:
$table = 'myTable';
$file = 'exportFile';
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= "\"" . $row['Field']."\",";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
I think this is my keeper. It goes to a CSV file with no issues, loads in Excel with no issues, it's my dream come true!
well, you can always use the same markup in excel as it can render html if you use the .xls file type, render the html table and change the content type to "application/vnd.ms-excel" specify a filename for that response as *.xls and you should have a usable excel sheet.
If there are no dobule quotes in the data, you can wrap the content of each cell in double quotes so your data looks like:
"...","...","..."
Now you can have commas in the data. You may also need to deal with new lines and returns in the data, probably best to remove them completely but that's up to you.
Thanks for all suggestions. As stated in my edited post, this script below was a great solution:
$table = 'myTable';
$file = 'exportFile';
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= "\"" . $row['Field']."\",";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
I think this is my keeper. It goes to a CSV file with no issues, loads in Excel with no issues, it's my dream come true!

Categories