Code for converting mysql data to excel output
- $ 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, Data Table: $ 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 \ n ");
- $ Sep = "\ t ";
- For ($ I = 0; $ I <mysql_num_fields ($ result); $ I ++ ){
- Echo mysql_field_name ($ result, $ I). "\ t ";
- }
- Print ("\ n ");
- $ I = 0;
- While ($ row = mysql_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. = "\ t ";
- Print (trim ($ schema_insert ));
- Print "\ n ";
- $ I ++;
- }
- Return (true );
- ?>
|
Mysql, excel