What is the Codeigniter equivilent to PHP multi_qu

2019-07-12 02:10发布

问题:

Whith PHP - I write a multi_query something like this:

$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?

If there is no equivilent - how would I use multi_query() in Codeigniter?

回答1:

You can use Codeigniter function $this->db->update_batch(), to which you can pass either an array or object.

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:



回答2:

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