Php 使用insert PDO MySQL获取insert id
我开始掌握PDO的基本知识 但是,我正在尝试获取插入行的id,我使用:Php 使用insert PDO MySQL获取insert id,php,sql,pdo,Php,Sql,Pdo,我开始掌握PDO的基本知识 但是,我正在尝试获取插入行的id,我使用: $query = $system->db->prepare("INSERT INTO {$this->_table} (name,description) VALUES (:name,:description)"); $query->execute(array('name'=>$name,'description'=>$description)); 我遇到的教程是关于事务的,但是我没有使
$query = $system->db->prepare("INSERT INTO {$this->_table} (name,description) VALUES (:name,:description)");
$query->execute(array('name'=>$name,'description'=>$description));
我遇到的教程是关于事务的,但是我没有使用事务 您可能正在寻找。“返回最后插入的行或序列值的ID”
使用事务时请注意 如果在调用
commit
之后调用lastInsertedId
,lastInsertedId
将返回0而不是id。
调用lastInserteded
就在execute
之后,但在commit
之前
$this->db->beginTransaction();
$this->stmt->execute();
$id = $this->db->lastInsertId();
$this->db->commit();
啊哈,谢谢!出于某种原因,我试图在$query变量上调用它。
$this->db->beginTransaction();
$this->stmt->execute();
$id = $this->db->lastInsertId();
$this->db->commit();