Use PHP to convert a mysql data table to an excel file format

Source: Internet
Author: User
& Lt ;? Php (as the mainstream development language) & #36; DB_Serverlocalhost; & #36; DB_Usernamemydowns; & #36; DB_Password; & #36; DB_DBNamemydowns; & #36; DB_TBLNameuser; & #36; Connect @ MySQL (the best combination with PHP )_ $ DB_Server = "localhost ";
$ DB_Username = "mydowns ";
$ DB_Password = "";
$ DB_DBName = "mydowns ";
$ DB_TBLName = "user ";
$ Connect = @ MySQL (the best combination with PHP) _ connect ($ DB_Server, $ DB_Username, $ DB_Password)
Or die ("Couldnt connect .");
$ Db = @ MySQL (the best combination with PHP) _ select_db ($ DB_DBName, $ Connect)
Or die ("Couldnt 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, Data Table: $ DB_TBLName, backup date: $ now_date ";
$ SQL = "Select * from $ DB_TBLName ";
$ ALT_Db = @ MySQL (the best combination with PHP) _ select_db ($ DB_DBName, $ Connect)
Or die ("Couldnt select database ");
$ Result = @ MySQL (the best combination with PHP) _ query ($ SQL, $ Connect)
Or die (MySQL (the best combination with PHP) _ error ());
Echo ("$ title ");
$ Sep = "";
For ($ I = 0; $ I <MySQL (the best combination with PHP) _ num_fields ($ result); $ I ++ ){
Echo MySQL (the best combination with PHP) _ field_name ($ result, $ I )."";
}
Print ("");
$ I = 0;
While ($ row = MySQL (the best combination with PHP) _ fetch_row ($ result ))
{
$ Schema_insert = "";
For ($ j = 0; $ 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.