tags:

views:

299

answers:

5
+1  Q: 

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. Any ideas? Thanks much.

+2  A: 

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.

Zoidberg
A: 

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.

sobedai
A: 

Google had some answers

Pickle
Ya, I've tried these -- thanks.
swt83
A: 

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);
swt83
A: 

The > '.$file.' redirection should be the last (or first) part of the command

Álvaro G. Vicario