_spaz Posted September 28, 2009 Share Posted September 28, 2009 Is it possible to export SQL results from multiple tables to CSV format? I have a query that joins multiple tables in a database and I would like it to export that data to CSV, here's my SQL query: SELECT MediaFile.id, MediaFile.filename, MediaFile.createtime, JobMediaFile.starttime, JobMediaFile.endtime, JobMediaFile.progress, Job.name, SummaryAlert.title, FileStatus.status FROM MediaFile LEFT JOIN JobMediaFile ON JobMediaFile.mediafile_id = MediaFile.id LEFT JOIN Job ON JobMediaFile.job_id = Job.id LEFT JOIN SummaryAlert ON JobMediaFile.id = SummaryAlert.jobmediafile_id LEFT JOIN FileStatus ON FileStatus.Mediafile_id = MediaFile.id GROUP BY MediaFile.id Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/ Share on other sites More sharing options...
jjacquay712 Posted September 28, 2009 Share Posted September 28, 2009 I had made a script to do this exact thing a few months ago. When I get home I'll try to dig it up and post it. Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/#findComment-926500 Share on other sites More sharing options...
jon23d Posted September 28, 2009 Share Posted September 28, 2009 fputcsv($resource, $fields), handles this beautifully! Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/#findComment-926538 Share on other sites More sharing options...
_spaz Posted September 29, 2009 Author Share Posted September 29, 2009 fputcsv($resource, $fields), handles this beautifully! Great thanks, this is the code that I'm currently using and it works great. Is there a way to display the Field names for each column? <?php mysql_connect("192.168.0.184","root","vtec19"); // Select the database $db_select=mysql_select_db("CerifyDB"); define('CSV_SEPERATOR',';'); define('CSV_PATH','\\'); define('CSV_FILENAME','data.csv'); $_POST[keyword] = Canwest; $result = mysql_query("SELECT MediaFile.id, MediaFile.filename, MediaFile.createtime, JobMediaFile.starttime, JobMediaFile.endtime, JobMediaFile.progress, Job.name, SummaryAlert.title, FileStatus.status FROM MediaFile LEFT JOIN JobMediaFile ON JobMediaFile.mediafile_id = MediaFile.id LEFT JOIN Job ON JobMediaFile.job_id = Job.id LEFT JOIN SummaryAlert ON JobMediaFile.id = SummaryAlert.jobmediafile_id LEFT JOIN FileStatus ON FileStatus.Mediafile_id = MediaFile.id WHERE MediaFile.filename LIKE '%$_POST[keyword]%' GROUP BY MediaFile.id"); $fp = fopen(CSV_FILENAME, 'w'); if ($fp == false) { die("Could not open data.csv for writing"); } while ($record = mysql_fetch_row($result)) { fputcsv($fp, $record); } echo '<a href="' . CSV_PATH . CSV_FILENAME . '" target="_blanc">CSV File</a>'; fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/#findComment-926795 Share on other sites More sharing options...
jon23d Posted September 29, 2009 Share Posted September 29, 2009 $row_counter = 0; if (mysql_num_rows($results)) { while ($r = mysql_fetch_assoc($results)) { if ($row_counter++ == 0) fputcsv(array_keys($file, $r); fputcsv($file, $r); } } Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/#findComment-927161 Share on other sites More sharing options...
_spaz Posted September 30, 2009 Author Share Posted September 30, 2009 $row_counter = 0; if (mysql_num_rows($results)) { while ($r = mysql_fetch_assoc($results)) { if ($row_counter++ == 0) fputcsv(array_keys($file, $r); fputcsv($file, $r); } } Thanks, works great! Link to comment https://forums.phpfreaks.com/topic/175815-mysql-to-csv-export/#findComment-927390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.