ghurty Posted June 19, 2009 Share Posted June 19, 2009 I have a php script that makes a lot of entries into a sql table. The problem is that each time it runs it erases the previous entries. What code can I put in the script so that when it finishes running, it will export the table (preferably as a CSV) and save it with a file name based on date/time? Thanks Link to comment https://forums.phpfreaks.com/topic/162879-howcode-to-automatically-export-sql-table-as-csv-save-file-name-by-datetime/ Share on other sites More sharing options...
Prismatic Posted June 19, 2009 Share Posted June 19, 2009 Here's a really terrible way to generate a CSV and dump it to the browser <?php error_reporting(E_NONE); /** Connection stuff... * * * */ /** Your table here */ $table = ""; $query = mysql_query("SELECT * FROM {$table} LIMIT 1"); foreach(mysql_fetch_assoc($query) as $key => $val) $csv_col[] = '"'. $key .'"'; $cols = implode(", ", $csv_col) ."\n"; $query = mysql_query("SELECT * FROM {$table}"); while($row = mysql_fetch_row($query)) { foreach($row as $val) { $csv_row[] = '"'. trim($val) .'"'; } $rows .= implode(", ", $csv_row) ."\n"; $csv_row = array(); } print_r($cols.$rows); ?> Link to comment https://forums.phpfreaks.com/topic/162879-howcode-to-automatically-export-sql-table-as-csv-save-file-name-by-datetime/#findComment-859447 Share on other sites More sharing options...
ghurty Posted June 19, 2009 Author Share Posted June 19, 2009 Thank you, But the php script is now being called by a browser, rather by an asterisk AGI script. I dont need to view the file when generated, rather just saved. Thanks Link to comment https://forums.phpfreaks.com/topic/162879-howcode-to-automatically-export-sql-table-as-csv-save-file-name-by-datetime/#findComment-859448 Share on other sites More sharing options...
ghurty Posted June 19, 2009 Author Share Posted June 19, 2009 If it it does noable as a CSV, then a sql file would be good enough. Thanks Link to comment https://forums.phpfreaks.com/topic/162879-howcode-to-automatically-export-sql-table-as-csv-save-file-name-by-datetime/#findComment-859658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.