<?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  
} 
?>
<?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.

Not bad, but try out my code

<?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!:`(

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”;
}
<?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)

please help!

how to upload a CSV file to my system using PHP

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

Member Avatar for diafol

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.