我想從我選擇的表中獲取最後一個ID。我知道lastinsertid();
sould工作,但它不斷返回0
。我在這裏做錯了什麼?PDO連接從表中選擇最後一個ID
public function getlastadded()
{
$Sql = 'SELECT * FROM agendaevent';
$Stm = $this->getEntityManager()->getConnection()->prepare($Sql);
$Stm->execute();
return $this->getEntityManager()->getConnection()->lastInsertId('id');
}
ps我在symfony工作,如果這有所作爲。
lastinsertid()插入後的作品,而不是選擇。你爲什麼需要它? –
我正在添加一個事件,但事件是鏈接的兩種方式,所以我必須將該ID添加到事件中。我不知道它只在插入後才起作用。@ YourCommonSense – Laurence
不,他的意思是*你*在選擇後調用'lastInsertId()',而不是INSERT。 IIRC lastInsertId()僅適用於上次創建元素的連接 –