Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用insert PDO MySQL获取insert id_Php_Sql_Pdo - Fatal编程技术网

Php 使用insert PDO MySQL获取insert 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)); 我遇到的教程是关于事务的,但是我没有使

我开始掌握PDO的基本知识

但是,我正在尝试获取插入行的id,我使用:

$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();