dotn30471 2016-03-31 10:00
浏览 10

添加尝试pdo执行

Code below adds data in db

    $sth = $this->db->prepare('UPDATE `adwords_clients_google` set status = 2');
    $sth->execute();
    $sth = null;  


    $sth = $this->db->prepare('
        INSERT INTO
            `adwords_clients_google`
            (`client_foreign_id`, `status`, `client_name`, `client_currency`)
        VALUES
            (:id, 1, :name, :currency)
        ON DUPLICATE KEY UPDATE
            `status` = VALUES(`status`),
            `client_name` = VALUES(`client_name`),
            `client_currency` = VALUES(`client_currency`)
    ');
    $sth->bindParam(':id', $id);
    $sth->bindParam(':name', $name);
    $sth->bindParam(':currency', $currency);
    foreach($accounts as $account) {
        $id = $account->customerId;
        $name = $account->name;
        $currency = $account->currencyCode;
        $sth->execute();         
    }

and I would like to add try here, something like

        try {
        if ($sth->execute()) {
            helper::putToLog('ok queryCampaignArr, inserted rows: ' . $sth->rowCount());
        } else {
            helper::putToLog('not ok', true);
        }
    } catch (Exception $ex) {
        helper::putToLog($sth->debugDumpParams(), true);
        helper::putToLog("ERROR: ".$ex->getMessage(), true);
    }

but i don't know should I add it for every row? How can I do that?

  • 写回答

1条回答 默认 最新

  • dongxing2030 2016-03-31 10:34
    关注

    If you are using PDO for connecting DB then use PDOException class to handle the exception.

    try {
        if ($sth->execute()) {
            helper::putToLog('ok queryCampaignArr, inserted rows: ' . $sth->rowCount());
        } else {
            helper::putToLog('not ok', true);
        }
    } catch (PDOException  $ex) {
        $Exception->getMessage(); // Error message
       (int)$Exception->getCode(); // Error Code
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看