Reputation: 3025
I have this table in csv file
Now i have two mysql tables dealing with this csv file,
Data A, B, C has to get stored in Table1
whereas data D, E, F, G, H has to get stored in Table2
.
I have the above formatted csv file, how can i upload its data to MYSQL
database??
So that from same file input can be done for different tables.
Upvotes: 1
Views: 8592
Reputation: 4069
Load the data in a temporary table
, then split up in two tables
.
Then drop
that temporary table
. I somewhere read that php can do this, but I cannot help in php codes.
Upvotes: 0
Reputation:
Try this it's working well, you can add as many values as possible depending on the number of columns you have in the CSV file.
<?php
$fname = $_FILES['csv_file']['name'];
$chk_ext = explode(".",$fname);
$filename = $_FILES['csv_file']['tmp_ name'];
$handle = fopen($filename, "r");
if(!$handle){
die ('Cannot open file for reading');
}
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$query = "INSERT INTO tablename (col1_csv, col2_csv)
values ('$data[0]', '$data[1]');
mysql_query($query)
or die(mysql_error());
$query1 = "INSERT INTO tablename (col1_csv, col2_csv)
values ('$data[0]', '$data[1]');
mysql_query($query1)
or die(mysql_error());
}
fclose($handle);
?>
Upvotes: 1
Reputation: 4331
Probably only via custom script (PHP or so)...
But it's usually not hard to split CSV into 2 files and import both into separate tables using LOAD_DATA function from MySQL directly. It will be MUCH faster than using phpMyAdmin or similar scripts.
Upvotes: 1