我尝试上传文本文件以插入到mysql中。它适用于少量行,但当行数超过1000时,它就会挂起。
我的探索是:
代码:
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$t = "0";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$mobile=$data[0];
$mobile = clean_value(mobile_filter($mobile));
if (preg_match('/^[0-9]{12,12}$/i',$mobile)) {
$check_mob = mysql_query("SELECT * FROM book WHERE mobile = '$mobile' AND owner = '$cp_username' AND groupname = '$groupname'");
if(mysql_num_rows($check_mob) == 0) {
$import="INSERT into book(nickname, mobile, groupname, owner) values('$nickname','$mobile','$groupname','$cp_username')";
mysql_query($import) or die(mysql_error());
$t = $t+1;
echo "<div class=\"progress\">
<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"$t\" aria-valuemin=\"0\" aria-valuemax=\"100\" style=\"width:$t%\">
$t%
</div>
</div>";
}
}
}
答案 0 :(得分:0)
你实际上也可以只使用PHP来做到这一点。
这是一个示例代码。
$sql="INSERT INTO .....";
$count++;
if($count==500){
// wait for 5 seconds
usleep(5000000);
$count=0;
}
您需要将此代码放在逐行保存到数据库中的循环中。当系统保存前500条记录时,它将休眠5秒并继续。