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. 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 expor开发者_Go百科t 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. I want the exported files to be available to the users of my site for download. The site it written in 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. 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.
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. 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 <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.
Third option: run query with mysql client and output text:
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
.
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". Then remove the semi-colon from "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.
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.
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);
?>
精彩评论