Im getting to grips with the basics of PDO.
我正在掌握PDO的基础知识。
However Im trying to get the id of the inserted row, Im using:
但是,我试图获取插入行的ID,我使用:
$query = $system->db->prepare("INSERT INTO {$this->_table} (name,description) VALUES (:name,:description)");
$query->execute(array('name'=>$name,'description'=>$description));
The tutorials I have come across are regarding transactions, however I am not using transactions!
我遇到的教程是关于交易的,但我没有使用交易!
2 个解决方案
#1
33
You're probably looking for lastInsertId. "Returns the ID of the last inserted row or sequence value".
你可能正在寻找lastInsertId。 “返回最后插入的行或序列值的ID”。
$insertedId = $system->db->lastInsertId() ;
#2
10
Pay attention when using transactions.
使用交易时要注意。
If you call lastInsertedId
after you call commit
, lastInsertedId
will return 0 instead of the id. Call lastInsertedId
right after execute
, but before commit
.
如果在调用commit后调用lastInsertedId,则lastInsertedId将返回0而不是id。在执行之后,但在提交之前调用lastInsertedId。
$this->db->beginTransaction();
$this->stmt->execute();
$id = $this->db->lastInsertId();
$this->db->commit();
#1
33
You're probably looking for lastInsertId. "Returns the ID of the last inserted row or sequence value".
你可能正在寻找lastInsertId。 “返回最后插入的行或序列值的ID”。
$insertedId = $system->db->lastInsertId() ;
#2
10
Pay attention when using transactions.
使用交易时要注意。
If you call lastInsertedId
after you call commit
, lastInsertedId
will return 0 instead of the id. Call lastInsertedId
right after execute
, but before commit
.
如果在调用commit后调用lastInsertedId,则lastInsertedId将返回0而不是id。在执行之后,但在提交之前调用lastInsertedId。
$this->db->beginTransaction();
$this->stmt->execute();
$id = $this->db->lastInsertId();
$this->db->commit();