I am trying to export mysql data into csv file,but the problem is that the csv file shows the name of fields not the fields data in csv file.
Here is my php code
<?php
error_reporting(0);
include"../connection.php";
$output = fopen('php://output', 'w');
$query = "SELECT 'claim_dateclaim','claim_clientname', 'claim_account', 'claim_contract', 'claim_parcelno', 'claim_refno', 'claim_datecollected', 'claim_coladdress', 'claim_colpostcode', 'claim_counsineaddres', 'claim_conpostcode', 'claim_retailvalue', 'claim_claimvalue', 'claim_protitle', 'claim_reason', 'claim_type', 'claim_repparcelno' FROM tbl_claimdetail ";
$rows = mysql_query($query );
fputcsv($output, array('Claim Date Raised','Client Name','Account no','Contract Number','Parcel Number','Company','Claim Ref no','Collection Date', 'Collection Address','Collection Postal code','Consignee Address','Consignee Post Code','Retail value','Claim value','Product','Reason','Type','Replacement no'));
while ($row = mysql_fetch_assoc($rows)) {
fputcsv($output, $row);
}
fclose($output);
header("Content-type: text/csv; charset=utf-8'");
header("Content-Disposition: attachment; filename=discussdesk".date('dmY').".csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
And here is my csv file
[![enter image description here][1]][1]
[1]: http://i.stack.imgur.com/9GNh6.jpg
use:
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
fputcsv($fp, array_values($row));
}
associative array will work with array_values
Related
I am a beginner student of PHP and i am doing some practice by coding a script to generate a CSV file.
Once that the csv file is generated , the format is correct as I've expected :
See the attached :
But if i add the download option the csv is generated wrongly with all the PHP page (i can see all the code)
Here the picture of the wrong csv
Could you help me to find the issue ?
Here the code :
<?php
include("db.php");
session_start();
$sql = "SELECT isocode FROM test_geo ORDER BY isocode";
$result_csv = mysqli_query($conn, $sql);
//facebook csv creation //
$filename = 'test.csv';
$fp = fopen($filename, 'w');
$headers = ['key', 'country'];
fputcsv($fp, $headers);
foreach ($result_csv as $key => $id) {
$new = array_push($id, "country");
fputcsv($fp, $id);
var_dump($id);
}
fclose($fp);
?>
Try this
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=result_file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
foreach ($new as $row) {
fputcsv($outputt, $row);
}
fclose($outputt);
exit();
I believe you put the header statements at the top of your script, so the download data includes the HTML codes too (like below...)
<!DOCTYPE html><html lang="en"><meta charset=...... >
You may use ob_flush() and set header (at the appropriate position of the code) so that the file will be correctly downloaded as csv when it is generated.
Here is the code:
<?php
session_start();
include("db.php");
$sql = "SELECT isocode FROM test_geo ORDER BY isocode";
$result_csv = mysqli_query($conn, $sql);
$data=array();
while($row = mysqli_fetch_array($result_csv, MYSQLI_ASSOC)) {
$ok=$row["isocode"] . "," . "Country";
array_push($data ,$ok);
}
ob_flush();
$filename="ok1.csv";
$csvData = join("\n", $data);
header('Content-Disposition: attachment; filename="'.$filename.'";');
header('Content-Type: application/csv; charset=UTF-8');
die($csvData);
?>
I have this code to get date from my database und get this as a csv file:
<?php
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$output = fopen('php://output', 'w');
$sql = 'SELECT * FROM `myTable`';
$result = $db->query( $sql );
while ($zeile = $result->fetch_object()) {
$myData[] = array (
'column_A' => $zeile->value_A,
'column_B' => $zeile->value_B
);
}
foreach ($myData as $data) {
fputcsv($output, $data, ";");
}
?>
This works fine !
But now I need a file, which has more the one sheet.
CSV can't handle this, so I need a xlsx file instead a csv file.
How can I export my data (like above) as a xlsx file with different sheets ?
Thanks !!
What I have done is get the data from MySQL to excel in csv format but I am not getting the entities of that table like if I have a table member having these 3 entities
memberid membername memberemail
I am getting the data of these entities like
1 alishah test#test.com
What I want is when I download an excel it gets header with it like
memberid membername memberemail
1 alishah test#test.com
What I did for excel is
<?php
$selectUserData = "SELECT * FROM tbl_member";
$export = $conn->query($selectUserData);
$fp= fopen('php://output', 'w');
foreach ($export as $fields)
{
fputcsv($fp, $fields);
}
header("Content-Type: text/csv;charset=utf-8" );
header("Content-Disposition: attachment;filename=member.csv");
header("Pragma: no-cache");
header("Expires: 0");
?>
Simply write a line with the column headers before writing the data to the csv.
<?php
$selectUserData = "SELECT * FROM tbl_member";
$export = $conn->query($selectUserData);
$fp = fopen('php://output', 'w');
fwrite($fp,'"memberid","membername","memberemail"'."\n");
foreach ($export as $fields)
{
fputcsv($fp, $fields);
}
header("Content-Type: text/csv;charset=utf-8" );
header("Content-Disposition: attachment;filename=member.csv");
header("Pragma: no-cache");
header("Expires: 0");
?>
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;
With PHP I'm opening a .csv file, I'm adding som rows in it then I'm downloading the new .csv file.
the code I'm using is:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen("csv.csv","r");
$list = array();
while(! feof($file))
{
$list[] = (fgetcsv($file));
}
fclose($file);
$list[] = array('name5', 'town5');
$list[] = array('name6', 'town6');
$list = array_filter($list);
outputCSV($list);
function outputCSV($list) {
$output = fopen("php://output", "w");
foreach ($list as $row) {
fputcsv($output, $row);
}
fclose($output);
}
My issue is the other PHP code in the page don't generate, and only the .csv file is being download.
for exemple if I'm adding:
echo "test";
it won't be display and only csv.csv will be downloaded
How can I display my "echo test;" ?
With the headers you are telling the browser to download a CSV, there is no where to output your echo.
You could make a page with the information you want to show that has an iframe with the csv code in it.