我尝试通过PHP加载csv文件,我的csv文件有很多记录(300)。加载csv文件过程时,它仅上传25行,然后PDO错误出现“连接过多”。有什么方法可以加快插入过程吗?
while(($emapData = fgetcsv($file, 10000, ",")) !== FALSE){
try{
$dbcon = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbcon->prepare("INSERT INTO Palm_Growth(palm_id, vgm_date, vgm_stage, rachis_length, petiole_length) VALUES(nullif('$emapData[0]','NULL'), nullif('$emapData[1]','NULL'), nullif('$emapData[2]','NULL'), nullif('$emapData[3]','NULL'), nullif('$emapData[4]','NULL')");
$stmt->execute();
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
} }
在调用插入语句之前先设置数据库连接$dbcon
。
$dbcon = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
while(($emapData = fgetcsv($file, 10000, ",")) !== FALSE){
try{
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $dbcon->prepare("INSERT INTO Palm_Growth(palm_id, vgm_date, vgm_stage, rachis_length, petiole_length) VALUES(nullif('$emapData[0]','NULL'), nullif('$emapData[1]','NULL'), nullif('$emapData[2]','NULL'), nullif('$emapData[3]','NULL'), nullif('$emapData[4]','NULL')");
$stmt->execute();
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}