最近使用pdo,遇到lastInsertId返回一直是0的问题,查找了很多资料,最后终于解决,返回正确id,需要达到的条件是:
1.主键为自增字段;
2.如果使用了事务,则调用lastInsertId需在事务commit之前;
lastInsertId在php手册中的描述是:
Note:
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even support the notion of auto-increment fields or sequences.
也就是说不支持设置自动递增字段的数据库,调用lastinsertId获取到的都是无意义的0值。