天天看點

擷取mysql中的資料并轉化為txt或者excel文檔

1. 輸出所有使用者的物品

$uids = sql_fetch_rows("select uid from sys_user");

foreach($uids as $uid)

{      

        $goodText = $uid["uid"];

$goodNames = sql_fetch_rows("select name  from sys_goods where uid = $uid"); 

        foreach(){

           $goodText .= "/t".$goodNames["name"];

       }

        file_put_contents('/yekai/good_data.txt',$goodText."/n",FILE_APPEND);

}

mysql設定字元集格式:

mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");

2. 轉化為Execl檔案并打包

   需要将上面的txt檔案轉化為excel檔案

   iconv函數把utf-8編碼的頁面轉成gb2312:$fileName = iconv("utf-8", "gb2312//IGNORE", $fileName);

   function convertTxtToExcel($fileName)

 {

$command = "tr -s '/n' '/r/n' < $fileName.txt|tr -s '/t' ','|iconv -c -f utf8 -t gb2312 > $fileName.csv";

system($command);

$command = "rm -f $fileName.txt";

system($command);

}

  function zipData()

{

$command = "zip `date +%Y%m%d`.zip *.csv";

system($command);

}

date指令:

     date -s 25/11/10 16:00:00

     date -s 25/11/10

     date -s 16:00:00

     date -d yesterday 設定為昨天的日期