开发者

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';
$c开发者_C百科md .= ' --lock-tables=false --no-create-info --tab=/tmp --fields-terminated-by=\',\'';
exec($cmd);

Everything I try creates an empty CSV file. Any ideas? Thanks much.


I found a way to accomplish this using mysql via command line --

        $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.

Here is a link to a download of the free community version

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...

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?

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.' redirection should be the last (or first) part of the command

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜