DamienRoche Posted February 1, 2009 Share Posted February 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/ Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/#findComment-752241 Share on other sites More sharing options...
DamienRoche Posted February 1, 2009 Author Share Posted February 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/#findComment-752249 Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/#findComment-752255 Share on other sites More sharing options...
DamienRoche Posted February 2, 2009 Author Share Posted February 2, 2009 Thank you. Sorry, I thought the semicolon within the speech marks on line 1 was an error. Is there any way to view what is executed in phpmyadmin when you export? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/#findComment-752259 Share on other sites More sharing options...
corbin Posted February 2, 2009 Share Posted February 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143413-is-there-an-sql-query-for-exporting-to-csv-like-phpmyadmin-function/#findComment-752261 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.