如何在 php 中插入数据库后获取最后一个 ID
how to get last id after insert db in php
这里我将多行数据保存到数据库中,我想检索每行的最后一个 ID,我该怎么做?
ex : 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
)
)
提前感谢那些回答的人
希望我说的够清楚
为什么不逐行插入而不是批量插入?
从每个插入构建一个字典,以便稍后将其配对。
$data = array();
$numrow = 1;
foreach ($sheet as $row) {
if ($numrow > 1) {
array_push($data, array(
'name' => $row['A'],
'description' => $row['B'],
));
}
$numrow++;
}
$array_of_insert_id = array();
foreach ($data as $index => $data_to_insert) {
$this->db->insert('my_tabel', $data_to_insert);
$array_of_insert_id[$index] = $this->db->insert_id();
}
$data_1 = array();
$numrow2 = 1;
$counter = 0;
foreach ($sheet as $row) {
if ($numrow2 > 1) {
array_push($data_1, array(
'id_last' => $array_of_insert_id[$counter],
'id' => $row['E'],
'barang' => $row['F'],
));
}
$numrow2++;
$counter++;
}
//table field with input field data array
$table_data = [
'table_field1' => $input_data1,
'table_field2' => $input_data2,
];
//insert data in database
$this->db->insert('table_name',$table_data);
//after insert return id
return $inserted_id = $this->db->insert_id();
这里我将多行数据保存到数据库中,我想检索每行的最后一个 ID,我该怎么做?
ex : 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
)
)
提前感谢那些回答的人 希望我说的够清楚
为什么不逐行插入而不是批量插入? 从每个插入构建一个字典,以便稍后将其配对。
$data = array();
$numrow = 1;
foreach ($sheet as $row) {
if ($numrow > 1) {
array_push($data, array(
'name' => $row['A'],
'description' => $row['B'],
));
}
$numrow++;
}
$array_of_insert_id = array();
foreach ($data as $index => $data_to_insert) {
$this->db->insert('my_tabel', $data_to_insert);
$array_of_insert_id[$index] = $this->db->insert_id();
}
$data_1 = array();
$numrow2 = 1;
$counter = 0;
foreach ($sheet as $row) {
if ($numrow2 > 1) {
array_push($data_1, array(
'id_last' => $array_of_insert_id[$counter],
'id' => $row['E'],
'barang' => $row['F'],
));
}
$numrow2++;
$counter++;
}
//table field with input field data array
$table_data = [
'table_field1' => $input_data1,
'table_field2' => $input_data2,
];
//insert data in database
$this->db->insert('table_name',$table_data);
//after insert return id
return $inserted_id = $this->db->insert_id();