1
<?php
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.csv");
header("Pragma: no-cache");
header("Expires: 0");

require_once( "db.php" );
 $query = "SELECT * FROM name ORDER BY last";
 $result = mysql_query($query) or die(mysql_error());
 echo "ID,First,Middle,Last,Email\r\n"; //header
while($row = mysql_fetch_array($result)){
echo "\"$row[id]\",\"$row[first]\",\"$row[middle]\",\"$row[last]\",\"$row[email]\"\r\n"; //data  
} 
?>
7
Contributors
8
Replies
20
Views
9 Years
Discussion Span
Last Post by diafol
0
<?php
$filename ="excelreport.csv";
$contents = "testdata1;testdata2;testdata3; \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
 ?>

I had given a similar example in another thread. Well, csv or excel, the procedure is the same.

0
<?php
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.csv");
header("Pragma: no-cache");
header("Expires: 0");

require_once( "db.php" );
 $query = "SELECT * FROM name ORDER BY last";
 $result = mysql_query($query) or die(mysql_error());
 echo "ID,First,Middle,Last,Email\r\n"; //header
while($row = mysql_fetch_array($result)){
echo "\"$row[id]\",\"$row[first]\",\"$row[middle]\",\"$row[last]\",\"$row[email]\"\r\n"; //data  
} 
?>

May i know what is with the 'db.php'?
and what is the name of the database for the ORDER table??please help!:`(

0

I think the below code will help you..

i found it in website

//
// establish database connection
//
$conn = mysql_connect( ‘MYSQL_HOST’, ‘MYSQL_USERNAME’, ‘MYSQL_PASSWORD’ ) or die( mysql_error( ) );
mysql_select_db( ‘MYSQL_DATABASE’, $conn ) or die( mysql_error( $conn ) );
//
// execute sql query
//
$query = sprintf( ‘SELECT * FROM MYSQL_TABLE’ );
$result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );
//
// send response headers to the browser
// following headers instruct the browser to treat the data as a csv file called export.csv
//
header( ‘Content-Type: text/csv’ );
header( ‘Content-Disposition: attachment;filename=export.csv’ );
//
// output header row (if atleast one row exists)
//
$row = mysql_fetch_assoc( $result );
if ( $row )
{
echocsv( array_keys( $row ) );
}
//
// output data rows (if atleast one row exists)
//
while ( $row )
{
echocsv( $row );
$row = mysql_fetch_assoc( $result );
}
//
// echocsv function
//
// echo the input array as csv data maintaining consistency with most CSV implementations
// * uses double-quotes as enclosure when necessary
// * uses double double-quotes to escape double-quotes
// * uses CRLF as a line separator
//
function echocsv( $fields )
{
$separator = ”;
foreach ( $fields as $field )
{
if ( preg_match( ‘/\\r|\\n|,|”/’, $field ) )
{
$field = ‘”‘ . str_replace( ‘”‘, ‘”"‘, $field ) . ‘”‘;
}
echo $separator . $field;
$separator = ‘,’;
}
echo “\r\n”;
}
0
<?php
$filename ="excelreport.csv";
$contents = "testdata1;testdata2;testdata3; \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
 ?>

I had given a similar example in another thread. Well, csv or excel, the procedure is the same.

Where did this man go? He is missing (together with kkeith and other longtime phpers)

0

Thakns Boss,
This article is very usefull.
php: Create CSV file..
I want to upload csv file.
So please advise me

0

You've necroposted to this thread. Your request is not related to creating a csv. Please start a new thread if you can't find a related thread from the Daniweb search.

You probably won't find an exact match as most files are uploaded to the server in the same ways. 2 main methods:

Form file field - thousands of tutorials about this online
FTP functions - see the PHP manual (php.net)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.