First, the header information is defined to indicate that an Excel is output. Then in the form of a table to the database information loop echo out, just fine.
Copy Code code as follows:
<?php
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=xls_region.xls");
$cfg _dbhost = ' localhost ';
$cfg _dbname = ' TestDB ';
$cfg _dbuser = ' root ';
$cfg _dbpwd = ' root ';
$cfg _db_language = ' UTF8 ';
End configuration
//linked Database
$link = mysql_connect ($cfg _dbhost, $cfg _dbuser, $cfg _dbpwd);
mysql_select_db ($cfg _dbname);
//Select encoding
mysql_query ("Set names". $cfg _db_language);
//users Table
$sql = "desc users";
$res = mysql_query ($sql);
echo "<table><tr>";
//Export Headers (that is, fields owned in the table)
while ($row = mysql_fetch_array ($res)) {
$t _field[] = $row [' The F in Field ']; //field is uppercase, otherwise there will be no result
echo ' <th> '. $row [' Field ']. " </th> ";
}
echo "</tr>";
//Export 100 data
$sql = "SELECT * from users limit";
$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>";