【问题标题】:CodeIgniter UPDATE query not working with column value additionCodeIgniter UPDATE 查询不适用于列值添加
【发布时间】:2017-08-10 09:44:43
【问题描述】:

在带有查询生成器的 CodeIgniter 更新中工作正常,但在我的情况下,我必须以下列方式运行多个更新查询。

$query="";
foreach($result as $row)    
{
  //generate query by some procedure will look like
  //UPDATE `accounts` SET `lastbal` = lastbal + 500.00 WHERE `id` = 1;
  $balance=$row['balance];
  $accountid=$row['acid];
  //append string
  $query.= "UPDATE `accounts` SET `lastbal` = lastbal + $balance WHERE `id` = $accountid";

}

所以 $query 就像在循环之外

$query=UPDATE `accounts` SET `lastbal` = lastbal + 500.00 WHERE `id` = 1;
UPDATE `accounts` SET `lastbal` = lastbal + 200.00 WHERE `id` = 2;
UPDATE `accounts` SET `lastbal` = lastbal + 60.00 WHERE `id` = 3;

执行

$this->db->query($query);

我收到了一些错误,比如缺少参数 blah ...blah

QUERY 在 SQL 控制台 上运行良好。是否可以在批处理模式下执行此查询。

我不想以下列方式在循环内运行查询

$this->db->set('lastbal', "lastbal+$balance", FALSE);
$this->db->where('id', $acid);
$this->db->update('accounts');

我希望它在 column_old_value+input 应该可用的批处理模式下。

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    如果您不想在循环内运行查询,那么您必须使用

    活动记录$this->db->update_batch()

    但不幸的是,在update_batch 中没有像今天这样的参数选项来禁用转义值,所以

    任一

    在查询前使用这一行:

    // set this to false so that _protect_identifiers skips escaping:
    
    // Codeigniter 3.x.x
    $this->db->protect_idenifiers(FALSE);
    
    // Codeigniter 2.x.x
    $this->db->_protect_identifiers=FALSE;
    

    这将停止向已构建的查询添加反引号。

    并提供如下输入

    $data = array();    
    
    foreach($result as $row)    
    {
        $data[] = array(
              'id' => $row['acid'] ,
    
              /* Give input as string */
              'lastbal'=>  'lastbal +' . $row['balance'] 
        )
    }
    
    $this->db->update_batch('accounts', $data, 'id'); 
    
    // important to set this back to TRUE or ALL of your queries from now on will be non-escaped:
    // Codeigniter 2.x.x
    $this->db->_protect_identifiers=TRUE;
    
    // Codeigniter 3.x.x
    $this->db->protect_idenifiers(TRUE);
    

    请注意以上内容不会逃避您的查询

    通过修改核心试试这个,只要给定的值是数组,它就会跳过转义,这样你的查询的其余部分就会用protect_identifiers转义

    https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_query_builder.php#L2098

    功能性

    public function set_update_batch($key, $index = '', $escape = NULL)
    {
    ..
    ..
    ..
    }
    

    来自

    'value'=>($escape === FALSE ? $v2 : $this->escape($v2))
    

    收件人

    'value'=>(is_array($v2) ? $v2[0] : ($escape === FALSE ? $v2 : $this->escape($v2)))
    

    并提供如下输入

    $data = array();    
    
    foreach($result as $row)    
    {
        $data[] = array(
              'id' => $row['acid'] ,
    
              /* Give input as array */
              'lastbal'=> array( 'lastbal +' . $row['balance'] )
        )
    }
    
    $this->db->update_batch('accounts', $data, 'id'); 
    

    在旧的 CI 2.2.x 上

    修改 DB_active_rec.php

    来自

                if ($escape === FALSE)
                {
                    $clean[$this->_protect_identifiers($k2)] = $v2;
                }
                else
                {
                    $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
                }
    

    收件人

     $clean[$this->_protect_identifiers($k2)] = (is_array($v2)? $v2[0] : ( ($escape === FALSE) ? $v2 : $this->escape($v2) ));
    

    【讨论】:

      【解决方案2】:

      使用活动记录$this->db->update_batch()

      $data = array();    
      
      foreach($result as $row)    
      {
          $data[] = array(
          'id' => $row['acid'] ,
          'lastbal'=> 'accounts'.'lastbal' +  $row['balance']
          )
      }
      
      $this->db->update_batch('accounts', $data, 'id'); 
      

      【讨论】:

        猜你喜欢
        • 2012-09-20
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 2021-09-03
        • 1970-01-01
        相关资源
        最近更新 更多