David's Free Code Snippits
Php, Html, Css, Javascript Snippits...
» Categories «
Ajax
Command
Cronjob
Css
Csv
Curl
Easysiteplugins
Expressions
Functions
Htaccess
Html
Javascript
Links
Mixed
Mysql
Notes
Other
Paypal
Php
Phparrays
Security
Sessions
Xml
Csv (Found:
1
)
1. Create CSV file for download from a Database Table
<?php $host = 'localhost'; $user = 'mysqlUser'; $pass = 'myUserPass'; $db = 'myDatabase'; $table = 'products_info'; $file = 'export'; $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 ".$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"); echo $csv_output; exit; ?>