简体   繁体   English

如何使用PDO :: lastInsertId()?

[英]How to use PDO::lastInsertId()?

$sql='INSERT INTO complaints(complaint_id) values(default)';
$db->query($sql,array());

//get newly added complaint_id

$complaint_id=$db->lastInsertId();

I used above code to get last insert ID from complaints table. 我使用上面的代码从投诉表中获取了最后一个插入ID。 But it gives an error like Fatal error: Call to undefined method EMMACore\\Utils\\DBConnection::lastInsertId() in /h... Can anybody tell me what is wrong in my application.Thanks. 但这会产生类似Fatal error: Call to undefined method EMMACore\\Utils\\DBConnection::lastInsertId() in /h...谁能告诉我我的应用程序有什么问题。 Fatal error: Call to undefined method EMMACore\\Utils\\DBConnection::lastInsertId() in /h...

In most case, the PDO instance is a property of EMMACore\\Utils\\DBConnection , so check the source code and find it out. 在大多数情况下,PDO实例是EMMACore\\Utils\\DBConnection的属性,因此请检查源代码并找出来。

Something like: $db->getDbh()->lastInsertId(); 类似于: $db->getDbh()->lastInsertId(); where getDbh (or something like that) returns the PDO instance. 其中getDbh (或类似的东西)返回PDO实例。

Edit: After seeing your result of var_dump() , that is for sure. 编辑:看到您的var_dump()结果之后,这是肯定的。

class db extends PDO

例如,您可以参考此链接PHP PDO Wrapper Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM