1
我根据ID将多个行从一个表插入另一个表。 对于此项目,我对所有数据库查询都使用PDO。这是我正在使用的代码/功能:无法从MySQL获取最后插入的ID
protected function importData($data) {
$i = 0;
$this->db->beginTransaction();
foreach($data as $item) {
$id = $item['id'];
$sql .= "INSERT INTO table1 (name,age)
SELECT name, age
FROM table12
WHERE id = $id; ";
$this->db->exec($sql);
$i++;
}
$this->db->commit();
// None of these are working
$last_id1 = $this->db->exec('SELECT LAST_INSERT_ID()');
$last_id2 = $this->db->lastInsertId();
echo 'id1: '.$last_id1.', id2:'.$last_id2;
}
但是对于某些重新生成,我无法获取最后插入的ID。 如果我尝试Toad for MySQL
中的SELECT LAST_INSERT_ID()
,我确实得到了一个结果,但它不是最后插入的行的ID。
为什么插入行时没有注册最后插入的行ID?
是因为我使用beginTransaction
和commit
,因此它被作为一个事务处理?