swt83
swt83

Reputation: 2011

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.

Upvotes: 2

Views: 3800

Answers (4)

Álvaro González
Álvaro González

Reputation: 146578

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

Upvotes: 0

swt83
swt83

Reputation: 2011

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);

Upvotes: 5

mote
mote

Reputation: 1449

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.

Upvotes: 0

Zoidberg
Zoidberg

Reputation: 10333

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.

Upvotes: 2

Related Questions