简体   繁体   English

MySQL DUMP作为CSV

[英]MySQL DUMP as CSV

I've looked around and nothing seems to work: 我环顾四周,似乎没有任何效果:

$file = '/path/to/file.csv';
$cmd = 'mysqldump DATABASE TABLE > '.$file.' --host=localhost --user=USER --password=PASS';
$cmd .= ' --lock-tables=false --no-create-info --tab=/tmp --fields-terminated-by=\',\'';
exec($cmd);

Everything I try creates an empty CSV file. 我尝试的所有操作都会创建一个空CSV文件。 Any ideas? 有任何想法吗? Thanks much. 非常感谢。

I found a way to accomplish this using mysql via command line -- 我找到了一种通过命令行使用mysql完成此操作的方法-

        $file = '/path/to/file.csv';
        if(is_file($file))
            unlink($file);      
        $sql = 'SELECT * FROM database.table';
        $cmd = 'mysql --host=localhost --user=USER --password=PASS --quick -e \''.$sql.'\' > '.$file;
        exec($cmd);

Try SQLYog, works like a charm. 尝试使用SQLYog,它的魅力十足。

Here is a link to a download of the free community version 这是免费社区版本的下载链接。

http://www.softpedia.com/progDownload/SQLyog-Community-Edition-Download-82252.html http://www.softpedia.com/progDownload/SQLyog-Community-Edition-Download-82252.html

Then all you have to do is right click on the table in question, and go export, and select CSV etc... 然后您要做的就是右键单击有问题的表,然后进行导出,然后选择CSV等...

If your looking for the code how to do it, then look in the history tab after you export the table and see what code was executed. 如果您在寻找代码的实现方法,请在导出表后查看历史记录选项卡,并查看执行了哪些代码。

what happens when you do: mysqldump DATABASE TABLE > /path/to/file.csv [snipped the rest of the command] from the command line? 当您执行以下操作时会发生什么:mysqldump DATABASE TABLE> /path/to/file.csv [从命令行中其余部分截取]?

Does it work? 它有用吗?

Your last line try: 您的最后一行尝试:

$cmd .= " --lock-tables=false --no-create-info --tab=/tmp --fields-terminated-by=','";

the single quotes don't get escaped in a single quoted string. 单引号不会在单引号字符串中转义。

The > '.$file.' > '.$file.' redirection should be the last (or first) part of the command 重定向应该是命令的最后(或第一)部分

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

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