PHP inserts data into the MySQL database in a loop. How to determine whether each item is inserted successfully?
阿神
阿神 2017-06-30 09:53:58
0
6
913

Just like the title.

            while(! feof($file)){
                $data= fgets($file);//fgets()函数从文件指针中读取一行
                $res = $this->Model->function($data);//插入数据的方法
            }
            //比如这种的怎么判断每一行都插入成功
阿神
阿神

闭关修行中......

reply all(6)
学习ing

//Define variable num
$num = 0;
while (! feof($file)) {

$data= fgets($file);//fgets()函数从文件指针中读取一行
$res = $this->Procesratio->promction($data);

// 直接在这里判断
if (!$res) {
    $num += 1;
}

}

// Judge the value of num here
if ($num == 0) {

// 全部成功

} else {

// $num即为失败条数

}

刘奇

Start the transactiontransaction, and as long as there is an error, it will rollback and throw an exception

巴扎黑
if($res == true){
   echo "success";
}else{
    echo "fault";
}
巴扎黑
while(! feof($file)){
    $data= fgets($file);//fgets()函数从文件指针中读取一行
    $res = $this->Model->function($data);//插入数据的方法
    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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template