在MySQLi LOAD DATA INFILE上发出问题以加载.CSV文件

时间:2015-07-27 23:31:08

标签: php mysql sql mysqli

我正在尝试使用MySQLi和PHP将40 mg .CSV文件加载到MYSQL数据库中,但是在加载页面后我只收到The user update failed:消息(Witdout错误消息!)

<?PHP   
   define ( 'DB_HOST', 'localhost' );
   define ( 'DB_USER', 'root' );
   define ( 'DB_PASS', '' );
   define ( 'DB_NAME', 'map' );


$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$sql = "LOAD DATA INFILE 'C:/wamp/www/UP/Modified_Single.csv'
       INTO TABLE  `single-tbl` 
       FIELDS TERMINATED BY ','
       OPTIONALLY ENCLOSED BY '\"' 
       LINES TERMINATED BY '\n' 
       IGNORE 1 LINES;";

$result = mysqli_query($con, $sql);

if (mysqli_affected_rows($con) == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};

echo $message;
mysqli_close($con);

你能告诉我为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

你的成功考验是错误的。 mysqli_affected_rows()返回插入表中的行数,该行数应与CSV文件中的行数相同。我怀疑40-meg文件只有1行,因此对== 1进行测试是错误的。

如果您想知道查询是否成功,请测试$result

if ($result) {
    $message = "The data was successfully added!";
} else {
    $message = "The user update failed: " . mysqli_error($con);
}