使用php将mysql表从mysql导出到csv文件 [英] export mysql list of tables from mysql to csv file using php
问题描述
我想知道有人可以向我显示一个示例PHP代码,我如何将MySQL数据库中的大约50个表导出到CSV文件。我的数据库名称是samples,我在这个数据库下有大约49个表。我希望这个数据库下的每个表(大约有20,00行)被导出到一个csv文件。
I wonder if someone could actually show me a sample PHP code on how can i export around 50 tables in a MySQL database to a CSV file. My database name is "samples" and i have around 49 tables under this database. I want each tables (which has around 20,00 rows) under this database to be exported to a csv file.
谢谢你,期待有任何帮助。对不起,我在PHP中是非常新的。
Thank you and looking forward for any help. Sorry by the way, I'm very new in PHP.
推荐答案
如果您可以访问MySQL服务器,可以使用 SELECT INTO OUTFILE
为您做大部分工作:
If you have access to the MySQL server, you can use SELECT INTO OUTFILE
to do most of this for you:
SELECT * FROM my_table
INTO OUTFILE 'my_table.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
您可能希望有一个行分隔符 \r\\\
如果您使用Windows。
You may want to have a line delimiter of \r\n
if you're using Windows.
如果不指定生成的CSV的完整路径将其存入MySQL表的数据目录。
If you don't specify a full path to the resulting CSV file it goes into the data directory of the MySQL server right beside the tables.
这篇关于使用php将mysql表从mysql导出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!