简体   繁体   English

什么是等同于PHP multi_query的Codeigniter

[英]What is the Codeigniter equivilent to PHP multi_query

Whith PHP - I write a multi_query something like this: Whith PHP-我编写了一个multi_query像这样:

$MySQL_string .= "UPDATE `table` SET `name` = 'joe' WHERE `ID` = '1'";
$MySQL_string .= "UPDATE `table` SET `name` = 'Jane' WHERE `ID` = '2'";

if($DB->mysqli->multi_query($MySQL_string) === TRUE){
    echo"Query executed successfully<BR>";
}else{
    echo"Error executing query: ".$DB->mysqli->error;
    die();
}

What is the equivalent to PHP's multi_query() in Codeigniter? Codeigniter中的PHP的multi_query()等价于什么?

If there is no equivilent - how would I use multi_query() in Codeigniter? 如果没有等效项,我将如何在Codeigniter中使用multi_query()?

You can use Codeigniter function $this->db->update_batch() , to which you can pass either an array or object. 您可以使用Codeigniter函数$this->db->update_batch() ,可以将数组或对象传递给该函数。

To complete your example it could look like this: 要完成您的示例,它可能如下所示:

$data = array(
  array(
    'name' => 'Joe' ,
    'ID' => '1'
  ),
  array(
    'name' => 'Jane' ,
    'ID' => '2'
  )
);

and then use: 然后使用:

$this->db->update_batch('mytable', $data, 'ID');

more info here : 更多信息在这里

Maybe use transactions? 也许使用交易?

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete(); 

CodeIgniter's Approach to Transactions CodeIgniter的交易方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM