
PDO,如下代码是无效的
$db->beginTransaction(); $db->prepare('insert......') ->execute(); echo $db->lastInsertId(); 你永远也无法获得到lastInsertId的值。
请使用如下方法:
echo $db->lastInsertId('tableName_pkeyName_seq'); 1 20150517 2015 年 1 月 21 日 via Android 没commit? |
2 cevincheung OP @20150517 跟那个没关系……就是pg在获取lastinsertid的时候一定要指定seq完整名称。 |
3 a591826944 2015 年 1 月 21 日 @20150517 没有 commit 自增 ID 也会被占用的 |
4 cevincheung OP @a591826944 +10086 |
5 joyqi 2015 年 1 月 21 日 这不算一个坑吧,在手册里清楚的写了 Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter. |
6 willwen 2015 年 1 月 21 日 via iPhone 用RETURNING id |
7 cevincheung OP @willwen 这是个非常棒的功能! |