gpt4 book ai didi

codeigniter - CodeIgniter:插入多个记录而无需循环

转载 作者:行者123 更新时间:2023-12-03 09:15:48 29 4
gpt4 key购买 nike

可以在CodeIgniter Active Record中使用多个INSERT记录,而无需for,foreach等。

我当前的代码:

foreach($tags as $tag) {
$tag = trim($tag);
$data = array(
'topic_id' => $topic_id,
'user_id' => $user_id,
'text' => $tag
);
$this->db->insert('topic_tags', $data);
}

最佳答案

Codeigniter Activity 记录具有功能insert_batch,我认为这是您需要的

$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);

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

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

适用于Codeigniter 3.x和Codeigniter 2.2.6

更新了链接

insert_batch() for Codeigniter 3.x

insert_batch() for Codeigniter 2.x

关于codeigniter - CodeIgniter:插入多个记录而无需循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14946766/

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