First, define the header information to output an excel file. Then, echo the database information in the form of a table.
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: filename=xls_region.xls ");
$ Pai_dbhost = 'localhost ';
$ Pai_dbname = 'testdb ';
$ User_dbuser = 'root ';
$ Mongo_dbpwd = 'root ';
$ Pai_db_language = 'utf8 ';
// END Configuration
// Link to the database
$ Link = mysql_connect ($ pai_dbhost, $ pai_dbuser, $ pai_dbpwd );
Mysql_select_db ($ pai_dbname );
// Select Encoding
Mysql_query ("set names". $ pai_db_language );
// Users table
$ SQL = "desc users ";
$ Res = mysql_query ($ SQL );
Echo "<table> <tr> ";
// Export the header (that is, the fields in the table)
While ($ row = mysql_fetch_array ($ res )){
$ T_field [] = $ row ['field']; // F in Field must be capitalized; otherwise, no result is returned.
Echo "<th>". $ row ['field']. "</th> ";
}
Echo "</tr> ";
// Export 100 data records
$ SQL = "select * from users' limit 100 ";
$ Res = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ res )){
Echo "<tr> ";
Foreach ($ t_field as $ f_key ){
Echo "<td>". $ row [$ f_key]. "</td> ";
}
Echo "</tr> ";
}
Echo "</table> ";
?>