Mysql CodeIgniter更新查询不使用列值添加

Mysql CodeIgniter更新查询不使用列值添加,mysql,codeigniter,Mysql,Codeigniter,在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]; $

在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);
我遇到了一些错误,比如缺少参数等等

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

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

$this->db->set('lastbal', "lastbal+$balance", FALSE);
$this->db->where('id', $acid);
$this->db->update('accounts');
我希望它处于批处理模式,列旧值+输入应该可用。

使用活动记录

使用活动记录


如果不想在循环内部运行查询,则必须使用

活动记录

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

任一

public function set_update_batch($key, $index = '', $escape = NULL)
{
..
..
..
}
在查询之前使用此行:

// 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);
$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'); 
请注意,以上内容不会逃避您的查询

通过修改core来尝试这一点,只要给定的值是数组,它就会跳过转义,这样剩下的查询就会用protect\u标识符转义

功能的

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

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

'value'=>(is_array($v2) ? $v2[0] : ($escape === FALSE ? $v2 : $this->escape($v2)))
 $clean[$this->_protect_identifiers($k2)] = (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 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);
$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\u active\u rec.php

来自

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

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

如果不想在循环内部运行查询,则必须使用

活动记录

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

任一

public function set_update_batch($key, $index = '', $escape = NULL)
{
..
..
..
}
在查询之前使用此行:

// 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);
$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'); 
请注意,以上内容不会逃避您的查询

通过修改core来尝试这一点,只要给定的值是数组,它就会跳过转义,这样剩下的查询就会用protect\u标识符转义

功能的

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

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

'value'=>(is_array($v2) ? $v2[0] : ($escape === FALSE ? $v2 : $this->escape($v2)))
 $clean[$this->_protect_identifiers($k2)] = (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 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);
$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\u active\u rec.php

来自

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

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