homeslice Posted March 12, 2008 Share Posted March 12, 2008 HELP! (& hello!) First post here because I'm tearing what little hair I have left out. I need some help extracting data from a MySQL table and inserting it into a CSV file using a PHP script. I can do this. Problem is, I have a MySQL table with 8 fields. I only want to extract 2 of these fields and their results into the CSV file. Please help. Thanks Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/ Share on other sites More sharing options...
derrick1123 Posted March 12, 2008 Share Posted March 12, 2008 Can you post what you have so far? Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490341 Share on other sites More sharing options...
homeslice Posted March 12, 2008 Author Share Posted March 12, 2008 This is what I have so far. Its modified from some other code and exports perfectly the entire table <? // Connect database $database="fyp"; $table="siteform"; mysql_connect("localhost","root",""); mysql_select_db("fyp"); $result=mysql_query("select * from $table"); $out = ''; // Get all fields names in table "siteform" in database "fyp". $fields = mysql_list_fields(fyp,$table); // Count the table fields and put the value into $columns. $columns = mysql_num_fields($fields); // Put the name of all fields to $out. for ($i = 0; $i < $columns; $i++) { $l=mysql_field_name($fields, $i); $out .= '"'.$l.'",'; } $out .="n"; // Add all values in the table to $out. while ($l = mysql_fetch_array($result)) { for ($i = 0; $i < $columns; $i++) { $out .='"'.$l["$i"].'",'; } $out .="n"; } // Open file export.csv. $f = fopen ('export.csv','w'); // Put all values from $out to export.csv. fputs($f, $out); fclose($f); header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="export.csv"'); readfile('export.csv'); ?> Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490357 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 If you only want to put two of the fields in the table, only retrieve those two fields with your query: <?php $q = "select field1,field2 from $table"; $rs = mysql_query($q) or die("Problem with the query [$q]<br>" . mysql_error()); $fp = fopen('export.csv','w'); fwrite($fp,"field1,field2\n"); while ($rw = mysql_fetch_assoc($rs)) fwrite($fp,'"'.implode('","',$rw) . '"' . "\n"); fclose($fp); header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="export.csv"'); readfile('export.csv'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490387 Share on other sites More sharing options...
homeslice Posted March 12, 2008 Author Share Posted March 12, 2008 Nice, thanks Problem is, it asks me if I want to download or save the file. I didnt get that before. Any idea how to just create it? Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490456 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 Remove these lines: <?php header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="export.csv"'); readfile('export.csv'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490471 Share on other sites More sharing options...
homeslice Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks Ken, you're a great help. Can I ask one last thing. If I want just the field names and results to print to the screen as CSV data without creating a file, how would I do that? My first snippet of code does this. Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-490483 Share on other sites More sharing options...
homeslice Posted March 13, 2008 Author Share Posted March 13, 2008 Anyone? Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-491159 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2008 Share Posted March 13, 2008 Use "echo" instead of "fwrite". Ken Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-491187 Share on other sites More sharing options...
homeslice Posted March 13, 2008 Author Share Posted March 13, 2008 hi Ken, Thanks. I've changed the fwrite to echo but I get an error now saying Unexpected ',' on line 12. Is this all I have to do? Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-491243 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2008 Share Posted March 13, 2008 fwrite uses a certain syntax, echo uses another. You have to use the correct syntax. If you don't know the syntax, use the fine manual for fwrite() and echo. Ken Link to comment https://forums.phpfreaks.com/topic/95780-convert-mysql-to-csv-using-php/#findComment-491247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.