这里我将数据保存到数据库的多行中,我想检索每行的最后一个id,我该怎么做呢?
例如:print_r输入数据时结果。
Array
(
[0] => Array
(
[name] => metode 11
[description] => DESC 1
)
[1] => Array
(
[name] => metode 22
[description] => DESC 2
)
[2] => Array
(
[name] => metode 33
[description] => DESC 1
)
)
在数据库中
id | name | description
1 metode 11 desc 1
2 metode 22 desc 2
3 metode 33 desc 3
完成输入后,我想把每个id和这个我的代码
$data = array();
$numrow = 1;
foreach ($sheet as $row) {
if ($numrow > 1) {
array_push($data, array(
'name' => $row['A'],
'description' => $row['B'],
));
}
$numrow++;
}
$this->db->insert_batch('my_tabel', $data);
$myid =$this->db->insert_id();
//after insert i retrieve id using last_id() function, however, only last id is fetched, i want to fetch each id
$data_1 = array();
$numrow2 = 1;
foreach ($sheet as $row) {
if ($numrow2 > 1) {
array_push($data_1, array(
'id_last' => $myid,
'id' => $row['E'],
'barang' => $row['F'],
));
}
$numrow2++;
}
我想这样:
Array
(
[0] => Array
(
[last_id] => 1
[id] => 33
[barang] => ccc
)
[1] => Array
(
[last_id] => 2
[id] => 44
[barang] => dd
)
[2] => Array
(
[last_id] => 3
[id] => 55
[barang] => eee
)
)
提前感谢那些已经回答的人我希望我说的足够清楚
2条答案
按热度按时间bogh5gae1#
为什么不逐行插入而不是成批插入呢?从每次插入构建一个字典,以便稍后将其配对回来。
bihw5rsg2#