| | |
Export CSV TO Mysql and Import to CSV from MYsql in PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
| 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 | |||
Export CSV TO Mysql and Import to CSV from MYsql in PHP
Last edited by tellysk; 20 Days Ago at 12:56 am.
Import To mysql $contents = file ('filename.csv'); for($i=0; $i<sizeof($contents); $i++) { $string = "remove value"; $no = str_replace($string, "/", $contents[$i]); Print $no; $sql = mysql_query("insert into tablename (id) values ('$no')"); echo "<br>"; } Export to CSV require 'exportcsv.inc.php'; $table="tablename"; // This is the tablename that you want to export to csv from mysql. exportMysqlToCsv($table); function exportMysqlToCsv($table,$filename = 'filename.CSV') { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select * from $table"; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; /* for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))); $schema_insert .= $l; $schema_insert .= $csv_separator; } */// end for // $out = trim(substr($schema_insert, 0, -1)); // $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose ;) header("Content-type: text/x-csv"); //header("Content-type: text/csv"); //header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=$filename"); if($out) { echo $out; $table="table_csv"; mysql_query("TRUNCATE $table"); } exit; }
Similar Threads
- Automate CSV file import to MYSQL (MySQL)
- Import CSV's into Mysql DB (MySQL)
- Export Mysql data to CSV (also from diferent tables) (PHP)
- Problems with exporting from a mysql database to a .csv (PHP)
- Import selected data from .csv and directly import on send_it.asp (ASP)
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube



