Convert MySQL datasheet to excel file format with PHP

Source: Internet
Author: User
Author: mydowns Source: Grasp Time Website: http://www.85time.com, http://www.mydowns.com
The original posted address is as follows:
Http://www.mydowns.com/article_show.php?id=32
Detailed contents are as follows:
<?php
$DB _server = "localhost";
$DB _username = "mydowns";
$DB _password = "";
$DB _dbname = "mydowns";
$DB _tblname = "user";
$Connect = @mysql_connect ($DB _server, $DB _username, $DB _password)
Or Die ("couldn ' t connect.");
$Db = @mysql_select_db ($DB _dbname, $Connect)
Or Die ("couldn ' t select Database.");
$file _type = "Vnd.ms-excel";
$file _ending = "xls";
Header ("content-type:application/$file _type");
Header ("content-disposition:attachment; filename=mydowns. $file _ending ");
Header ("Pragma:no-cache");
Header ("expires:0");
$now _date = Date (' y-m-d h:i ');
$title = "Database name: $DB _dbname, Datasheet: $DB _tblname, Backup date: $now _date";
$sql = "SELECT * from $DB _tblname";
$ALT _db = @mysql_select_db ($DB _dbname, $Connect)
Or Die ("couldn ' t Select Database");
$result = @mysql_query ($sql, $Connect)
Or Die (Mysql_error ());
Echo ("$title");
$sep = "";
for ($i = 0; $i < Mysql_num_fields ($result); $i + +) {
Echo Mysql_field_name ($result, $i). " ";
}
Print ("");
$i = 0;
while ($row = Mysql_fetch_row ($result))
{
$schema _insert = "";
for ($j =0; $j <mysql_num_fields ($result); $j + +)
{
if (!isset ($row [$j]))
$schema _insert. = "NULL". $sep;
ElseIf ($row [$j]!= "")
$schema _insert. = "$row [$j]". $sep;
Else
$schema _insert. = "" $SEP;
}
$schema _insert = Str_replace ($sep.) $ "," ", $schema _insert);
$schema _insert. = "";
Print (Trim ($schema _insert));
Print "";
$i + +;
}
return (true);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.