gpt4 book ai didi

php - 在 Codeigniter 中运行多个分号分隔的查询

转载 作者:行者123 更新时间:2023-11-29 06:13:02 26 4
gpt4 key购买 nike

有什么方法可以在没有 insert_batch() 的情况下在 Code Igniter 中运行多个分号分隔的查询?

例如:

$a = 'INSERT INTO table (a,b,c) VALUES (1,2,3); INSERT INTO table1 (x,y,z) VALUES (1,2,3);';
$this->db->query($a);

以上代码给出了invalid query错误。

最佳答案

如果多个表只有一个数据(使用 Transactions )-(N 表 vs 1 数据)

Running Transactions Manually

$this->db->trans_begin();

$this->db->query('INSERT INTO table (a,b,c) VALUES (1,2,3)');
$this->db->query('INSERT INTO table1 (x,y,z) VALUES (1,2,3)');

if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}

Running Transactions Automatically

$this->db->trans_start();
$this->db->query('INSERT INTO table (a,b,c) VALUES (1,2,3)');
$this->db->query('INSERT INTO table1 (x,y,z) VALUES (1,2,3)');
$this->db->trans_complete();

如果插入包含多个数据的单表(1 表 vs N 数据)

$data = array(
array(
'a' => 'My title 1' ,
'b' => 'My Name 1' ,
'c' => 'My date 1'
),
array(
'a' => 'My title 2' ,
'b' => 'My Name 2' ,
'c' => 'My date 2'
)
);

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

关于php - 在 Codeigniter 中运行多个分号分隔的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com