Exporting mysql table to .txt or .doc file using PHP

前端 未结 5 504
借酒劲吻你
借酒劲吻你 2020-12-19 11:48

I have a mysql table that keeps a log of messages that users send every day. What I want to do is export the the message log once per day into a text file, and am not sure h

5条回答
  •  情歌与酒
    2020-12-19 12:17

    Be careful about rolling your own, unless you handle things like NULL, character sets, etc.

    First alternative:

    query("SELECT * FROM myTable INTO OUTFILE 'data.txt'");
    $dummy = $result->fetchAll(); 
    

    The data.txt file will be written on the MySQL server. The directory must be writable by the uid of the mysqld process. It will not overwrite any existing file, and requires that you have the FILE SQL privilege.

    Second alternative: use mysqldump to output to flat text file (as @OMG Ponies mentioned):

    mysqldump -t -T   

    This works like INTO OUTFILE, it needs to run on the MySQL server host, and the directory must be writable by the mysqld uid.

    Third option: run query with mysql client and output text:

    mysql -B -e "SELECT * FROM MyTable"  > mytable.txt
    

    This can be run on any host, and requires no special privileges or directory permissions. But NULL may not be handled as it would be with mysqldump or INTO OUTFILE.

    提交回复
    热议问题