I want to import a csv file content into a database table, I'm using this code that it works perfectly when pasting it in phpmyadmin console:
LOAD DATA LOCAL INFILE '/Applications/MAMP/htdocs/testApp/trips.csv' INTO TABLE trips FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS (type,startDate,endDate,steps,coordinates,distance)
However, when using it in a php file, I have an error:
<?php
$host = "localhost"; //Your database host server
$db = "dbTest"; //Your database name
$user = "root"; //Your database user
$pass = "root"; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if (!$connection) {
die("Database server connection failed.");
die(mysqli_error($db));
} else {
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if (!$dbconnect) {
die("Unable to connect to the specified database!");
} else {
$sql = "LOAD DATA LOCAL INFILE '/Applications/MAMP/htdocs/testApp/trips.csv'
INTO TABLE trips
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'
IGNORE 1 ROWS (type,startDate,endDate,steps,coordinates,distance)"
;
$result = mysql_query($sql, $connection);
if (mysql_affected_rows() == 1) {
$message = "The trip was successfully inserted!";
} else {
$message = "The trip insert failed";
$message .= mysql_error();
}
echo $message;
}
}
?>
=> The trip insert failed
I'm pretty sure that the problems come from a \
or any other character that I can't target.
Thank you for helping .
EDIT : You are mixing two plugins MYSQL and MYSQLi in same script, below i dumped remaining part of your code using mysqli plugin, because your upper part uses mysqli...
N.B:You can't do that. Pick just any single API.
$result = mysqli_query($connection, $sql);
if (mysqli_affected_rows($connection) >= 1) {
$message = "The trip was successfully inserted!";
} else {
$message = "The trip insert failed";
$message .= mysqli_error($connection);
}