2

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?

2 Answers2

3

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:

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • I see. The 'ID' in the batch statement acts as WHERE `ID` = '2' etc. And the batch uses WHEN `ID` = '2' THEN etc... Brilliant... I think 0_o I wonder how this compares in speed to PHP multi_query() and I wonder why Codeigniter did not include multi_query() as part of its query builders? –  Jan 11 '17 at 21:08
1

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

Mohammad
  • 497
  • 3
  • 15
  • Not sure I understand - why would I need a transaction? There would be no need to roll anything back in my case. –  Jan 11 '17 at 20:05
  • transactions : for half run queries . – Mohammad Jan 11 '17 at 20:08
  • Thank you - but there is no need for a transaction here. Unless that is the only option in Codeignter for multi_query(). I am just looking for the equivalent to multi_query() –  Jan 11 '17 at 20:11