zoukankan      html  css  js  c++  java
  • ***codeigniter 2.2 affected_rows()返回值不准确

    http://blog.icodeu.com/?p=596

    问题描述
    今天在完成一个项目调用想要检验一下计划插入的数据是否都正常插入了。调用insert_batch()方法插入一百多条数据的时候发现affected_rows()返回值不准确。
    问题分析
    1.第一步打印last_query()发现插入的数据和affected_rows()数值确实是一样的
    2.检查数据库,发现需要插入的一百多条数据也确实正常插入了
    3.查看了一下代码,才知道在insert_batch()和update_batch()的时候,如果数据量大于100条,将会分多次插入,每次最多只会插入一百条。
    详见下面的代码

    1
    2
    3
    4
    5
    6
    // Batch this baby
    for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
    {
        $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
        $this->query($sql);
    }

    影响范围
    insert_batch(),update_batch()
    解决方案
    对插入的数据进行求余然后和affected_rows()进行对比检验插入数据是否正确

    github上官方已经修复了这个问题

    https://github.com/bcit-ci/CodeIgniter/tree/2.2-stable

     
     
    1
    2
    3
    4
    5
    6
    7
        // Batch this baby
    $affected_rows = 0;
    for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
    {
    $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
    $affected_rows += $this->affected_rows();
    }
  • 相关阅读:
    洛谷1026 统计单词个数
    洛谷1381 单词背诵
    Manacher(hdu3068最长回文)
    洛谷P1078 文化之旅
    HDU1269 迷宫城堡
    codevs1020 孪生蜘蛛
    洛谷P1027 Car的旅行路线
    楼房 洛谷1382 && codevs2995
    Picture poj1177
    排列组合
  • 原文地址:https://www.cnblogs.com/kenshinobiy/p/4826288.html
Copyright © 2011-2022 走看看