Export php-mysql records into excel sheet
To export php-mysql records into excel sheet and download it follow these simple steps.
- Make database connection.
- Read the headers (fieldname of your table) and store in a variable.
- Read records one by one and store it in another variable.
- Set the file name to be downloaded.
- Set appropriate http header types to write data on excel sheet and download it.
<?php ob_start(); mysql_connect('localhost','username','password'); mysql_select_db('dbname'); $sql = "SELECT * from tablename"; $res = mysql_query($sql) or die(); $count = mysql_num_fields($res); // fetch table header from database $header = ''; for ($i = 0; $i < $count; $i++){ $header .= mysql_field_name($res, $i)."\t"; } // fetch data each row, store on tabular row data while($row = mysql_fetch_row($res)){ $line = ''; foreach($row as $value){ if(!isset($value) || $value == ""){ $value = "\t"; }else{ $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; $data = str_replace("\r", "", $data); } $name=date('d-m-y').'-list.xls'; header("Content-type:application/vnd.ms-excel;name='excel'"); header("Content-Disposition: attachment; filename=$name"); header("Pragma: no-cache"); header("Expires: 0"); // Output data echo $header."\n\n".$data; ?>
Export php-mysql records into excel sheet
Reviewed by JS Pixels
on
December 24, 2010
Rating:
thank you for ur great code!!! it useful for me!!!
ReplyDeleteDhaval Rajani
Thank u so much Altaf Hussain ........
ReplyDeletethank a lot.it work well
ReplyDeletethankyou very much for your useful code
ReplyDeleteIt work´s!!!! perfect, thank you for you easy and clear solution
ReplyDeletethank you very much..:)
ReplyDeletebut it in the same row and column how can export with on data store in on row or column
ReplyDeletethanks you...its work perfectly
ReplyDeleteit is exporting only the last row of the table. Please correct the code and post the new one.
ReplyDeleteHey thanks for the code. However I am getting this warning:
ReplyDelete"Cannot modify header information - headers already sent"
for the following piece of code:
header("Content-type:application/vnd.ms-excel;name='excel'");
header("Content-Disposition: attachment; filename=$name");
header("Pragma: no-cache");
header("Expires: 0");
i prefer to do it from phpmyadmin. just export the table as csv format from phpmyadmin and open it from excel. simple!
ReplyDeleteCould you advice regading while($row = mysql_fetch_row($res)){
ReplyDeleteWhere $row value is set?
The first instance of $data is concatenated.
ReplyDelete$data .= trim($line)."\n";
Shouldn't there be an instance of $data before this??
When I try to run this I'm getting an error that $data is undefined on that line. The period means that it is concatenating to an early instance of that variable..... So where is the earlier instance?
Seems like a line is missing
$result1=executeQuery("select s.stdname,s.emailid,IFNULL((select sum(q.marks) from studentquestion as sq,question as q where q.qnid=sq.qnid and sq.testid=".$_REQUEST['testid']." and sq.stdid=st.stdid and sq.stdanswer=q.correctanswer),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid'].";" );
ReplyDeleteI would like to execute these rows .
Please help me :(
But it wont give me the data ;( PLEASE HELP ASAP PLS
How can i download two tables in same excel sheet?
ReplyDelete