Related to your question, we're happy to have a few paths to resolve it:
1. LOAD DATA INFILE
A great MySQL feature that allows you to import a CSV file directly, in a single query.
The SQL command you're looking for is LOAD DATA INFILE
Manual page here: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Quick example:
LOAD DATA INFILE 'fileName'
INTO TABLE tableName
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(
field1,
field2,
field3,
@variable1,
@variable2,
etc
)
set
(
field4 = concat(@variable1,@variable2)
);
That's a fairly basic example, but it covers most of what you'd want. The manual page gives full details of how to do some very complex stuff with it.
**2. Parsing your CSV and inserting it programatically **
set_time_limit(10000);
$con = new mysqli("example.com", "user", "password", "database");
if (mysqli_connect_errno()) {
trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);}
$fp = fopen("file.csv", "r");
while( !feof($fp) ) {
if( !$line = fgetcsv($fp, 1000, ';', '"')) {
continue;
}
$importSQL = "INSERT INTO table_name VALUES('".$line[0]."','".$line[1]."','".$line[2]."')";
$qry = $con->query(($importSQL) ;
if($qry === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $con->error, E_USER_ ERROR);
}
}
fclose($fp);
Hope that helps.
LOCAL INFILEand another server is, if the file is on your maschine, not working