faqts

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 20 people (55%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do i show the results of mysql in excel sheet using PHP?

May 12th, 2008 20:49
i can do it, Vishal Rathod, amal k m, Deepak Bansal, Rachit Bagda, web developers and php programmers, shobhan challa, http://www.rachit.com http://www.sargems.com http://www.chrisranjana.com http://www.rtnaustralia.com http://www.rtnasia.com http://travel.kelkoo.co.uk/browse/b/172201/c_Amal+K+M.html


define(db_host, "YOUR DATABASE HOST");
define(db_user, "USERNAME");
define(db_pass, "PASSWORD");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "DB_NAME");
mysql_select_db(db_name);
/********************************************
Write the query, call it, and find the number of fields
/********************************************/
$select = "SELECT * FROM TABLE_NAME";				
$export = mysql_query($select);
$count = mysql_num_fields($export);
/********************************************
Extract field names and write them to the $header
variable
/********************************************/
for ($i = 0; $i < $count; $i++) {
	$header .= mysql_field_name($export, $i)."\t";
}
/********************************************
Extract all data, format it, and assign to the $data
variable
/********************************************/
while($row = mysql_fetch_row($export)) {
	$line = '';
	foreach($row as $value) {					
		if ((!isset($value)) OR ($value == "")) {
			$value = "\t";
		} else {
			$value = str_replace('"', '""', $value);
			$value = '"' . $value . '"' . "\t";
		}
		$line .= $value;
	}
	$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
/********************************************
Set the default message for zero records
/********************************************/
if ($data == "") {
	$data = "\n(0) Records Found!\n";				
}
/********************************************
Set the automatic downloadn section
/********************************************/
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=spreadsheet.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
http://www.stupidarticles.com
http://www.halazona.com
http://www.shikapika.com
http://www.stakoza.com
http://www.uploadarticles.com
http://www.ganazat.com
http://www.damima.com
http://www.tarabiza.com
http://www.articlesxarticles.com
http://www.articlesfreedirectory.com