phpcodeignitermulti-query

What is the Codeigniter equivilent to PHP multi_query


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?


Solution

  • 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: