Export Mysql data to CSV (also from diferent tables)

Reply

Join Date: Mar 2009
Posts: 1
Reputation: michaelG13 is an unknown quantity at this point 
Solved Threads: 0
michaelG13 michaelG13 is offline Offline
Newbie Poster

Export Mysql data to CSV (also from diferent tables)

 
0
  #1
Mar 13th, 2009
Hello,
Im new to that kind of code so forgive me.

I have 3 diferent tables

players:
id_player
name_player

players_details:
id_player
position
age

players_eval:
id_player
value
wage

You can see that the ‘id_player’ is in all tables. Now, what i want to do is export on csv and if the ‘id_player’ is the same number with the other tables to continew and show the rest of the info i have on the mysql. Also i would like when the 'id_player' to rename to Player ID on the excel file or 'age' to Age etc.

This is the code im using
  1. $table="players"; // this is the tablename that you want to export to csv from mysql.
  2.  
  3. function exportMysqlToCsv($table);
  4. {
  5. $csv_terminated = "\n";
  6. $csv_separator = ",";
  7. $csv_enclosed = '"';
  8. $csv_escaped = "\\";
  9. $sql_query = "select * from $table";
  10.  
  11. // Gets the data from the database
  12. $result = mysql_query($sql_query);
  13. $fields_cnt = mysql_num_fields($result);
  14.  
  15.  
  16. $schema_insert = '';
  17.  
  18. for ($i = 0; $i < $fields_cnt; $i++)
  19. {
  20. $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
  21. stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
  22. $schema_insert .= $l;
  23. $schema_insert .= $csv_separator;
  24. } // end for
  25.  
  26. $out = trim(substr($schema_insert, 0, -1));
  27. $out .= $csv_terminated;
  28.  
  29. // Format the data
  30. while ($row = mysql_fetch_array($result))
  31. {
  32. $schema_insert = '';
  33. for ($j = 0; $j < $fields_cnt; $j++)
  34. {
  35. if ($row[$j] == '0' || $row[$j] != '')
  36. {
  37.  
  38. if ($csv_enclosed == '')
  39. {
  40. $schema_insert .= $row[$j];
  41. } else
  42. {
  43. $schema_insert .= $csv_enclosed .
  44. str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
  45. }
  46. } else
  47. {
  48. $schema_insert .= '';
  49. }
  50.  
  51. if ($j < $fields_cnt - 1)
  52. {
  53. $schema_insert .= $csv_separator;
  54. }
  55. } // end for
  56.  
  57. $out .= $schema_insert;
  58. $out .= $csv_terminated;
  59.  
  60. } // end while
  61. $file = "testing";
  62. $filename = $file."_".date("Y-m-d_H-i",time());
  63. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  64. header("Content-Length: " . strlen($out));
  65. // Output to browser with appropriate mime type, you choose ;)
  66. //header("Content-type: text/x-csv");
  67. //header("Content-type: text/csv");
  68. header("Content-type: application/csv");
  69. header("Content-disposition: csv" . date("Y-m-d") . ".csv");
  70. //header("Content-Disposition: attachment; filename=$filename".csv");
  71. header( "Content-disposition: attachment; filename=".$filename.".csv");
  72. echo $out;
  73. exit;
  74. }

I really need your help

Thank you
Michael
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC