Export CSV TO Mysql and Import to CSV from MYsql in PHP

View Poll Results: code is useful?
yes 1 100.00%
no 0 0%
somewhat 0 0%
often 0 0%
Voters: 1. You may not vote on this poll

tellysk tellysk is offline Offline 31 Days Ago, 12:45 am |
0
Export CSV TO Mysql and Import to CSV from MYsql in PHP
Last edited by tellysk; 31 Days Ago at 12:56 am.
Quick reply to this message  
PHP Syntax
  1. Import To mysql
  2.  
  3. $contents = file ('filename.csv');
  4. for($i=0; $i<sizeof($contents); $i++)
  5. {
  6. $string = "remove value";
  7. $no = str_replace($string, "/", $contents[$i]);
  8. Print $no;
  9. $sql = mysql_query("insert into tablename (id) values ('$no')");
  10. echo "<br>";
  11. }
  12.  
  13.  
  14. Export to CSV
  15.  
  16. require 'exportcsv.inc.php';
  17.  
  18. $table="tablename"; // This is the tablename that you want to export to csv from mysql.
  19.  
  20. exportMysqlToCsv($table);
  21.  
  22.  
  23.  
  24.  
  25. function exportMysqlToCsv($table,$filename = 'filename.CSV')
  26. {
  27. $csv_terminated = "\n";
  28. $csv_separator = ",";
  29. $csv_enclosed = '"';
  30. $csv_escaped = "\\";
  31. $sql_query = "select * from $table";
  32.  
  33. // Gets the data from the database
  34. $result = mysql_query($sql_query);
  35. $fields_cnt = mysql_num_fields($result);
  36.  
  37.  
  38. $schema_insert = '';
  39.  
  40. /* for ($i = 0; $i < $fields_cnt; $i++)
  41.   {
  42.   $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i)));
  43.   $schema_insert .= $l;
  44.   $schema_insert .= $csv_separator;
  45.   } */// end for
  46.  
  47. // $out = trim(substr($schema_insert, 0, -1));
  48. // $out .= $csv_terminated;
  49.  
  50. // Format the data
  51. while ($row = mysql_fetch_array($result))
  52. {
  53. $schema_insert = '';
  54. for ($j = 0; $j < $fields_cnt; $j++)
  55. {
  56. if ($row[$j] == '0' || $row[$j] != '')
  57. {
  58.  
  59. if ($csv_enclosed == '')
  60. {
  61. $schema_insert .= $row[$j];
  62. } else
  63. {
  64. $schema_insert .= $csv_enclosed .
  65. str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
  66. }
  67. } else
  68. {
  69. $schema_insert .= '';
  70. }
  71.  
  72. if ($j < $fields_cnt - 1)
  73. {
  74. $schema_insert .= $csv_separator;
  75. }
  76. } // end for
  77.  
  78. $out .= $schema_insert;
  79. $out .= $csv_terminated;
  80. } // end while
  81.  
  82. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  83. header("Content-Length: " . strlen($out));
  84. // Output to browser with appropriate mime type, you choose ;)
  85. header("Content-type: text/x-csv");
  86. //header("Content-type: text/csv");
  87. //header("Content-type: application/csv");
  88. header("Content-Disposition: attachment; filename=$filename");
  89.  
  90. if($out)
  91. {
  92. echo $out;
  93. $table="table_csv";
  94. mysql_query("TRUNCATE $table");
  95. }
  96.  
  97.  
  98.  
  99. exit;
  100.  
  101.  
  102.  
  103. }
0
willempie willempie is offline Offline | 29 Days Ago
Good Job !!
 
 

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC