001<?php 002 003//EDIT YOUR MySQL Connection Info: 004$DB_Server="localhost";//your MySQL Server 005$DB_Username="";//your MySQL User Name 006$DB_Password="";//your MySQL Password 007$DB_DBName="";//your MySQL Database Name 008$DB_TBLName="";//your MySQL Table Name 009//$DB_TBLName, $DB_DBName, may also be commented out & passed to the browser 010//as parameters in a query string, so that this code may be easily reused for 011//any MySQL table or any MySQL database on your server 012 013//DEFINE SQL QUERY: 014//you can use just about ANY kind of select statement you want - 015//edit this to suit your needs! 016$sql="Select * from$DB_TBLName"; 017 018//Optional: print out title to top of Excel or Word file with Timestamp 019//for when file was generated: 020//set $Use_Titel = 1 to generate title, 0 not to use title 021$Use_Title=1; 022//define date for title: EDIT this to create the time-format you need 023$now_date=date('m-d-Y H:i'); 024//define title for .doc or .xls file: EDIT this if you want 025$title="DumpForTable$DB_TBLNamefromDatabase$DB_DBNameon$now_date"; 026/* 027 028Leave the connection info below as it is: 029just edit the above. 030 031(Editing of code past this point recommended only for advanced users.) 032*/ 033//create MySQL connection 034$Connect=@mysql_connect($DB_Server,$DB_Username,$DB_Password) 035ordie("Couldn't connect to MySQL:<br>".mysql_error()."<br>".mysql_errno()); 036//select database 037$Db=@mysql_select_db($DB_DBName,$Connect) 038ordie("Couldn't select database:<br>".mysql_error()."<br>".mysql_errno()); 039//execute query 040$result=@mysql_query($sql,$Connect) 041ordie("Couldn't execute query:<br>".mysql_error()."<br>".mysql_errno()); 042 043//if this parameter is included ($w=1), file returned will be in word format ('.doc') 044//if parameter is not included, file returned will be in excel format ('.xls') 045if(isset($w)&&($w==1)) 046{ 047$file_type="msword"; 048$file_ending="doc"; 049}else{ 050$file_type="vnd.ms-excel"; 051$file_ending="xls"; 052} 053//header info for browser: determines file type ('.doc' or '.xls') 054header("Content-Type: application/$file_type"); 055header("Content-Disposition: attachment; filename=database_dump.$file_ending"); 056header("Pragma: no-cache"); 057header("Expires: 0"); 058 059/* Start of Formatting for Word or Excel */ 060 061if(isset($w)&&($w==1))//check for $w again 062{ 063/* FORMATTING FOR WORD DOCUMENTS ('.doc') */ 064//create title with timestamp: 065if($Use_Title==1) 066{ 067echo("$title\n\n"); 068} 069//define separator (defines columns in excel & tabs in word) 070$sep="\n";//new line character 071 072while($row=mysql_fetch_row($result)) 073{ 074//set_time_limit(60); // HaRa 075$schema_insert=""; 076for($j=0;$j<mysql_num_fields($result);$j++) 077{ 078//define field names 079$field_name=mysql_field_name($result,$j); 080//will show name of fields 081$schema_insert.="$field_name:\t"; 082if(!isset($row[$j])){ 083$schema_insert.="NULL".$sep; 084} 085elseif($row[$j]!=""){ 086$schema_insert.="$row[$j]".$sep; 087} 088else{ 089$schema_insert.="".$sep; 090} 091} 092$schema_insert=str_replace($sep."$","",$schema_insert); 093$schema_insert.="\t"; 094print(trim($schema_insert)); 095//end of each mysql row 096//creates line to separate data from each MySQL table row 097print"\n----------------------------------------------------\n"; 098} 099}else{ 100/* FORMATTING FOR EXCEL DOCUMENTS ('.xls') */ 101//create title with timestamp: 102if($Use_Title==1) 103{ 104echo("$title\n"); 105} 106//define separator (defines columns in excel & tabs in word) 107$sep="\t";//tabbed character 108 109//start of printing column names as names of MySQL fields 110for($i=0;$i<mysql_num_fields($result);$i++) 111{ 112echomysql_field_name($result,$i)."\t"; 113} 114print("\n"); 115//end of printing column names 116 117//start while loop to get data 118while($row=mysql_fetch_row($result)) 119{ 120//set_time_limit(60); // HaRa 121$schema_insert=""; 122for($j=0;$j<mysql_num_fields($result);$j++) 123{ 124if(!isset($row[$j])) 125$schema_insert.="NULL".$sep; 126elseif($row[$j]!="") 127$schema_insert.="$row[$j]".$sep; 128else 129$schema_insert.="".$sep; 130} 131$schema_insert=str_replace($sep."$","",$schema_insert); 132//following fix suggested by Josue (thanks, Josue!) 133//this corrects output in excel when table fields contain \n or \r 134//these two characters are now replaced with a space 135$schema_insert=preg_replace("/\r\n|\n\r|\n|\r/"," ",$schema_insert); 136$schema_insert.="\t"; 137print(trim($schema_insert)); 138print"\n"; 139} 140} 141 142?>