[英]LOAD DATA INFILE error in php
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: 我想将一个csv文件内容导入数据库表中,我使用的是在phpmyadmin控制台中粘贴该代码时可以完美工作的代码:
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文件中使用它时,出现错误:
<?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
=>
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 . 感谢您的帮助 。
You've mixed up MySQL and MySQLi plugins — you're trying to connect with MySQLi then perform a query with MySQL. 您混合使用了MySQL和MySQLi插件-您尝试与MySQLi连接,然后对MySQL执行查询。 You can't do that.
你不能那样做。 Pick one API and use it consistently.
选择一个 API并持续使用它。
So: 所以:
mysql_query($sql) ---> $connection->query($sql)
mysql_affected_rows() ---> $result->affected_rows()
mysql_error() ---> $connection->error
Ultimately, this has nothing to do with LOAD DATA INFILE
and you should have tried it with a basic SELECT
! 最终,这与
LOAD DATA INFILE
无关,您应该使用基本的SELECT
进行尝试! And I recommend reading the documentation for the functions that you use. 并且我建议您阅读所用功能的文档 。
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... 编辑:您在同一脚本中混合使用两个插件MYSQL和MYSQLi,下面我使用mysqli插件转储了代码的其余部分,因为您的上部使用了mysqli ...
NB :You can't do that. 注意 :你不能那样做。 Pick just any single API.
仅选择任何一个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);
}
尝试你trip.csv文件移动到datadir
-您可以使用此命令找到它: SELECT @ @datadir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.