Php 从表中获取PDO lastInsertId()

Php 从表中获取PDO lastInsertId(),php,mysql,pdo,Php,Mysql,Pdo,是否可以从方法中获取lastInsertId(),如果可能,还可以从特定表中获取值 我尝试使用的方法是在我的db.class.php public function getLastID(){ $dbh = $this->db_connect(); if($dbh){ try{ $lastId = $dbh->lastInsertId(); return $lastId; } catch(PDOException $err){ return

是否可以从方法中获取
lastInsertId()
,如果可能,还可以从特定表中获取值

我尝试使用的方法是在我的
db.class.php

public function getLastID(){ 
 $dbh = $this->db_connect(); 
 if($dbh){ 
  try{
   $lastId = $dbh->lastInsertId(); 
   return $lastId; 
  } 
  catch(PDOException $err){
   return $err->getMessage(); 
  } 
 } 
 else{ 
  return false; 
 } 
}
当我访问该方法时,
echo$db->getLastID(),我得到的值是
0

return $db->lastInsertId('column');

stackoverflow.com/questions/10809752/how-to-use-pdolastinsertid


stackoverflow.com/questions/10809752/how-to-use-pdolastinsertid

但我无法将此添加到方法中并随时调用它。如果我理解正确,它需要处于相同的连接中。我在想一个可以在任何地方调用的方法,但我不能把它添加到一个方法中,并在任何时候调用它。如果我理解正确,它需要处于相同的连接中。我在想一种方法,我可以在任何地方调用它。
$db->getDbh()->lastInsertId();