我开始掌握PDO的基础知识。
但是,我正在尝试获取插入行的id,我使用:
$query = $system->db->prepare("INSERT INTO {$this->_table} (name,description) VALUES (:name,:description)");
$query->execute(array('name'=>$name,'description'=>$description));
我遇到的教程都是关于事务的,但是我没有使用事务!
发布于 2010-09-19 01:45:10
你可能在找lastInsertId。“返回最后插入的行或序列值的ID”。
$insertedId = $system->db->lastInsertId() ;
发布于 2013-10-25 06:53:27
使用事务时要注意。
如果在调用commit
之后调用lastInsertedId
,lastInsertedId
将返回0而不是id。在execute
之后但在commit
之前调用lastInsertedId
。
$this->db->beginTransaction();
$this->stmt->execute();
$id = $this->db->lastInsertId();
$this->db->commit();
https://stackoverflow.com/questions/3742554
复制相似问题