Blog > detail

Just my tiny mind

Import MySql From CSV

26-2-2012 Admin General 2 Comments

Import MySql From CSV

Continuing from my previous article about MySql Into CSV" href="http://septiadi.com/2011/05/30/export-mysql-into-csv/" target="_blank">Export MySql Into CSV, I will explain about how to Import MySql From CSV. In this case, you will need to load the CSV file by using a PHP file that placed in the destination server. The PHP file will read the CSV file and then make a MySql query related to the content of the CSV file.


Here the PHP file:


$host = 'localhost';//host name, in this case we use localhost

$user = 'root';//user name, in this case we use root

$pass = 'password';//password, in this case we use password

$db = 'member';//declare the database that will be used


$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());

mysql_select_db($db) or die("Can not connect.");


$handle = @fopen("csv.csv", "r");

$content = '';

if ($handle) {

while (($buffer = fgets($handle)) !== false) {

$content .= $buffer;

}

}

$arr1 = explode("\n",$content);

$table = '';

foreach($arr1 as $val){

preg_match_all('/(?


You can change the $q based on the table that you want to inser. In this case, the code above will only export-import one table. However, with little bit modification you can make your own code for export-import multiple table. Good luck.

Import MySql From CSV Import MySql From CSV Import MySql From CSV

Import MySql From CSV Import MySql From CSV Import MySql From CSV

Import <a href=MySql From CSV" width="1" height="1" />

word word

Import MySql From CSV

Import MySql From CSV

Import MySql From CSV