【问题标题】:What is the Codeigniter equivilent to PHP multi_query什么是与 PHP multi_query 等效的 Codeigniter
【发布时间】:2017-05-26 18:21:33
【问题描述】:

使用 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();
}

Codeigniter 中 PHP 的 multi_query() 等价于什么?

如果没有等价物 - 我将如何在 Codeigniter 中使用 multi_query()?

【问题讨论】:

    标签: php codeigniter multi-query


    【解决方案1】:

    也许使用交易?

    $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

    【讨论】:

    • 不确定我是否理解 - 为什么需要交易?就我而言,没有必要回滚任何东西。
    • 事务:用于半运行查询。
    • 谢谢 - 但这里不需要交易。除非这是 Codeignter 中用于 multi_query() 的唯一选项。我只是在寻找相当于 multi_query()
    【解决方案2】:

    您可以使用 Codeigniter 函数 $this-&gt;db-&gt;update_batch(),向其传递数组或对象。

    要完成您的示例,它可能如下所示:

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

    然后使用:

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

    更多信息here

    【讨论】:

    • 我明白了。批处理语句中的 'ID' 充当 WHERE ID = '2' 等。批处理使用 WHEN ID = '2' THEN 等......太棒了......我想 0_o 我想知道这个比较PHP multi_query() 的速度,我想知道为什么 Codeigniter 没有将 multi_query() 作为其查询构建器的一部分?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2011-03-11
    • 2010-11-27
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多