dpp78272 2014-10-23 01:49
浏览 50
已采纳

在foreach成功之后处理php错误

I tried catch if do CRUD in foreach loop, if success loop finish push message into array, below is my function, I push message after foreach is it correct?

// .. defined response_message array

try {
  foreach ($data as $i => $each_data) {
    $sql = "UPDATE tag_0 SET sequence = :sequence WHERE id = :id";
    $stmt = $connect_db_read->prepare($sql);
    $stmt->bindValue(':id', $each_data['id']);
    $stmt->bindValue(':sequence', $each_data['sequence']);
    $stmt->execute();
  }

  // here
  $success_message = 'success_message: store in database success';
  array_push($response_message['success_message'], $success_message);
} catch (PDOException $e) {
  $debug_message = $e->getMessage();
  array_push($response_message['debug_message'], $debug_message);
  $error_message = "error_message: ? database";
  array_push($response_message['error_message'], $error_message);
}


$data = array();
$data['response_message'] = $response_message;


return $data;
  • 写回答

1条回答 默认 最新

  • dongquanjie9328 2014-10-23 02:36
    关注

    $stmt->execute(); will return true when a query is succesful.

    This does not mean the record was updated , it only means that the query executed without error.

    To be really sure, you have to check the number of affected rows using rowCount()

    Try this approach:

    $error_counter = 0;
    $sql = "UPDATE tag_0 SET sequence = :sequence WHERE id = :id";
    $stmt = $connect_db_read->prepare($sql);  
    foreach ($data as $i => $each_data) {
        $stmt->bindValue(':id', $each_data['id']);
        $stmt->bindValue(':sequence', $each_data['sequence']);
        $status = $stmt->execute();
        $row_count = $stmt->rowCount();
        if($status && $row_count > 0){
            $success_message = 'Successful update for sequence id ='.$each_data['id'];
            array_push($response_message['success_message'], $success_message);
        }else{
            $failed_message = 'update failed for sequence id ='.$each_data['id'];
            array_push($response_message['fail_message'], $fail_message);
            $error_counter++;
        }
    }
    

    You should also prepare once than bind and execute in the loop, no need to send the query back to the db at each iteration.


    I added another way to track with a variable $error_counter if the value is 0 then all records where updated.
    //check after the loop
    if($error_counter === 0){
        echo 'All UPDATES SUCCESSED !!!!';
    }else{
        echo 'NOT ALL SUCCESFUL !';
    }
    

    Alternatively you can achieve the same using the count of the failed messages:

    (count($response_message['fail_message'])===0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理