Just like the title.
while(! feof($file)){
$data= fgets($file);//fgets()函數(shù)從文件指針中讀取一行
$res = $this->Model->function($data);//插入數(shù)據(jù)的方法
}
//比如這種的怎么判斷每一行都插入成功
閉關(guān)修行中......
//Define variable num
$num = 0;
while (! feof($file)) {
$data= fgets($file);//fgets()函數(shù)從文件指針中讀取一行
$res = $this->Procesratio->promction($data);
// 直接在這里判斷
if (!$res) {
$num += 1;
}
}
// Judge the value of num here
if ($num == 0) {
// 全部成功
} else {
// $num即為失敗條數(shù)
}
Start the transactiontransaction
, and as long as there is an error, it will rollback
and throw an exception
while(! feof($file)){
$data= fgets($file);//fgets()函數(shù)從文件指針中讀取一行
$res = $this->Model->function($data);//插入數(shù)據(jù)的方法
if($res){
echo "success!";
}else{
echo "error!。。。try again....";
$this->Model->function($data)
}
}
After the insertion is successful, data will be returned. Just make a judgment based on the return! !
For this large amount of data insertion method, if the online business is not so busy, you can consider inserting in batches. After batch inserting, the returned result is the number of affected rows. The returned results are easier to search and the efficiency will be higher than this. Or business, simple violence.
It is recommended to use things, so that you can at least ensure the consistency of the data.