Jump to content

Is there an sql query for exporting to csv? - like phpmyadmin function.


DamienRoche

Recommended Posts

I am able to export csv from phpmyadmin but can't find a simple command to do it with php. Yet obviously phpmyadmin runs on php. Everywhere I go, I getting php classes for it.

 

I can't see it being that complex. Is there an sql query for it?- namely:

 

export to csv for ms excel, put field names in first row.

 

Thanks.

Link to comment
Share on other sites

Have you tried anything?  It's not exactly hard x.x.

 

 

You could just do something as basic as:

 

$query = mysql_query("SELECT * FROM table;");

$first = true;

while($row = mysql_fetch_assoc($query)) {
    if($first) {
        $keys = array();
        foreach($row as $key => $value) {
            $keys[] = $key;
        }
        echo '"' . implode('", "', $keys) . '"' . PHP_EOL;
    }
    echo '"' . implode('", "', $row) . '"' . PHP_EOL;
}

Link to comment
Share on other sites

Please don't insult me. Of course I've tried. I still am trying and have been for the past 2 hours. Please read my sig. It isn't a lie.

 

That code just echoes data and echoes the table headers for every row and there is a

syntax error on line 1. Thanks any way.

 

It's just, I need something simple so I can learn from it. I've tried numerous php classes and

snippets and I can't get any of them to work because they seem hugely complex.

 

I found this sql query but have no idea how to use it:

 

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'

  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

  LINES TERMINATED BY '\n'

  FROM test_table;

 

I've put it in a mysql_query but nothing. Plugged my data into it but it doesn't seem

to output anything.

 

I'll keep experimenting. I welcome any other ideas. Thanks.

 

 

 

 

Link to comment
Share on other sites

Your first post gave me the impression that you just googled and didn't actually try anything your self.

 

 

How does my script have a syntax error on line one?  Line 1 looks perfectly fine to me.

 

 

 

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'

  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

  LINES TERMINATED BY '\n'

  FROM test_table;

 

Will select column a, column b, and the sum of column a and b and write it to /tmp/result.txt with fields terminated by ",", enclosed in "" and lines terminated by a return.

 

 

 

It doesn't output anything as far as mysql results go.  You will need to open the file that it writes.

Link to comment
Share on other sites

Do you mean the query/code PHPMyAdmin exports with?  I'm sure it's in the source code, but I don't think there's anyway to know besides that.

 

 

I would imagine PHPMyAdmin uses something along the lines of SELECT blah INTO OUTFILE when exporting as CSV based on the options it gives.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.