简体   繁体   English

使用 PHP 将 mysql 表导出为 .txt 或 .doc 文件

[英]Exporting mysql table to .txt or .doc file using PHP

I have a mysql table that keeps a log of messages that users send every day.我有一个 mysql 表,其中记录了用户每天发送的消息。 What I want to do is export the the message log once per day into a text file, and am not sure how to do this.我想要做的是每天一次将消息日志导出到文本文件中,但不知道该怎么做。 Our server has phpmyadmin, and I can export the table into a text file manually, but I don't know how to either A) have phpmyadmin export this file automatically once per day, or B) write the exporting in php code.我们的服务器有 phpmyadmin,我可以手动将表格导出到文本文件中,但我不知道如何 A)有 phpmyadmin 每天自动导出此文件一次,或 B)用 ZE1BFD762321E409CEE4AC 代码编写导出I want the exported files to be available to the users of my site for download.我希望导出的文件可供我网站的用户下载。 The site it written in php.它用 php 编写的站点。 If there is any other information you need to answer this question, let me know!如果您需要回答此问题的任何其他信息,请告诉我!

<?php
    $fh = fopen('data.txt', 'w');
    $con = mysql_connect("localhost","root","");
    mysql_select_db("db_name", $con);

    /* insert field values into data.txt */

    $result = mysql_query("SELECT * FROM table_name");   
    while ($row = mysql_fetch_array($result)) {          
        $last = end($row);          
        $num = mysql_num_fields($result) ;    
        for($i = 0; $i < $num; $i++) {            
            fwrite($fh, $row[$i]);                      
            if ($row[$i] != $last)
               fwrite($fh, ", ");
        }                                                                 
        fwrite($fh, "\n");
    }
    fclose($fh);
?>

This simple PHP script will save all the data in a table to a.txt file.这个简单的 PHP 脚本会将表格中的所有数据保存到 .txt 文件中。 Records will be separated by new lines, and fields by a tab character.记录将由新行分隔,字段由制表符分隔。 Here it is:这里是:

<?
$fh = fopen('data.txt', 'w');
mysql_connect('host', 'username', 'password');
$result = mysql_query("SELECT * FROM myTable;");
while ($row = mysql_fetch_array($result)) {
    $last = end($row);
    foreach ($row as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");
}
fclose($fh);
?>

Be careful about rolling your own, unless you handle things like NULL, character sets, etc.小心自己滚动,除非您处理 NULL、字符集等问题。

First alternative:第一种选择:

<?php
$pdo = new PDO(...);
$results = $pdo->query("SELECT * FROM myTable INTO OUTFILE 'data.txt'");
$dummy = $result->fetchAll(); 

The data.txt file will be written on the MySQL server. data.txt 文件将写入 MySQL 服务器上。 The directory must be writable by the uid of the mysqld process.该目录必须可由 mysqld 进程的 uid 写入。 It will not overwrite any existing file, and requires that you have the FILE SQL privilege.它不会覆盖任何现有文件,并且要求您具有FILE SQL 权限。

Second alternative: use mysqldump to output to flat text file (as @OMG Ponies mentioned):第二种选择:使用 mysqldump 到 output 到平面文本文件(如@OMG Ponies 所述):

mysqldump -t -T <directory> <database> <table>

This works like INTO OUTFILE , it needs to run on the MySQL server host, and the directory must be writable by the mysqld uid.这类似于INTO OUTFILE ,它需要在 MySQL 服务器主机上运行,并且该目录必须是 mysqld uid 可写的。

Third option: run query with mysql client and output text:第三个选项:使用 mysql 客户端和 output 文本运行查询:

mysql -B -e "SELECT * FROM MyTable" <database> > 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 .但是 NULL 可能无法像使用 mysqldump 或INTO OUTFILE那样处理。

The answer I had posted earlier will work perfectly only when last field value in a row is not equal to any other field value in that same row.Bcoz the checking for comma separation is based on the last field value,now it changed to last field index.我之前发布的答案只有在一行中的最后一个字段值不等于同一行中的任何其他字段值时才能完美运行。Bcoz 检查逗号分隔是基于最后一个字段值,现在它更改为最后一个字段指数。

If u use the previous code,then u cant get the proper comma separation near the field value which is equal to last field value in that row,other ways the code is also correct.如果您使用前面的代码,那么您无法在等于该行中最后一个字段值的字段值附近获得正确的逗号分隔,其他方式代码也是正确的。

<?php
    $fh = fopen('data.txt', 'w');
    $con = mysql_connect("localhost","root","");
    mysql_select_db("db_name", $con);

    /* insert field values into data.txt */

    $result = mysql_query("SELECT * FROM table_name");   
    while ($row = mysql_fetch_array($result)) {          
        $num = mysql_num_fields($result) ;    
        $last = $num - 1;
        for($i = 0; $i < $num; $i++) {            
            fwrite($fh, $row[$i]);                       
            if ($i != $last) {
                fwrite($fh, ",");
            }
        }                                                                 
        fwrite($fh, "\n");
    }
    fclose($fh);
?>

Well the main issue with this is script is that you did not select a database, that is, using "mysql_select_db" before "mysql_query".那么这个脚本的主要问题是你没有 select 一个数据库,也就是说,在“mysql_query”之前使用“mysql_select_db”。 Then remove the semi-colon from "myTable(;)".然后从“myTable(;)”中删除分号。 Also create a blank text file within your root folder, this is where your data would be stored.Your script should work fine afterwards.还要在根文件夹中创建一个空白文本文件,这是存储数据的地方。之后您的脚本应该可以正常工作。 This solution is for future users who might need this script to aid their design.此解决方案适用于可能需要此脚本来帮助他们设计的未来用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM