I am trying to get all rows from csv file to insert the data to MySQL database. Now I have this csv file:
name1;country1
name2;country2
and to get the data I use PHP, like this:
<?php
$db_host = 'ip';
$db_user = 'user';
$db_pass = 'pass';
$database = 'database_name';
if (!mysql_connect($db_host, $db_user, $db_pass))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Database doesn't exist");
$handle = fopen("csvfile.csv", "r");
if( $handle ) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$import = "INSERT INTO table (name, country, updated) VALUES ('$data[0]', '$data[1]', now())";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
}
?>
It is working but only with the first row of the csv file. When I do the select of the table on MySQL the result is only the first row of the csv. The second row is not inserting.
How Can I do to insert all the rows correctly?