[英]Importing Large CSV file in MySQL using php
I am trying to import a 60,000 or more rows in a CSV file. 我想在CSV文件中导入60,000或更多行。 The code can import a 5000 lines only. 代码只能导入5000行。 Someone can help me? 有人可以帮帮我吗?
require_once('connection.php');
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO transactions (EntryId_Pk, AccessCode, MobileNumber, TelcoCode, TIN, ORNo, ORAmt, NumOfEntries, ECN, AddedOn) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
You might want to use the LOAD DATA
MySQL statement. 您可能想要使用LOAD DATA
MySQL语句。 This can be really fast since you don't need to read everything into PHP-land and let MySQL be smart about the allocations. 这可能非常快,因为您不需要将所有内容都读入PHP-land并让MySQL对分配有所了解。 You can use it something like this from PHP: 你可以在PHP中使用这样的东西:
// dont use mysql_* anymore in new code
mysqli_query($dblink, '
LOAD DATA LOCAL INFILE "'.$file.'"
INTO TABLE transactions
FIELDS TERMINATED by ","
OPTIONALLY ENCLOSED BY "\'"
LINES TERMINATED BY "\n"
');
It could be a timeout error. 这可能是超时错误。 Try to set 尝试设置
set_time_limit(0);
Also could be a good practice to import the data in chunks and insert multiple rows within a query instead of doing it row by row. 也可以是以块的形式导入数据并在查询中插入多行而不是逐行执行的良好实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.