获取最后一个 ID PDO

Getting last id PDO

这是我的:

$stmt = $handler->prepare('SELECT id FROM users');
$stmt->execute();
$id = $handler->lastInsertId();

echo $id;

所以,我希望结果是 "There is $id registered users"。

您永远不需要 select 来自 table 的 "last id"。每次你认为你需要它时,你需要别的东西。例如,"Last id" 与注册用户数无关。相反,您必须 count 用户:

$count = $handler->query('SELECT count(id) FROM users')->fetchColumn();
echo "There is $count registered users";

PDO::lastInsertId -- Returns the ID of the last inserted row or sequence value.
https://secure.php.net/manual/en/pdo.lastinsertid.php.

您想要的是 count 个注册用户,参见 Row count with PDO

要检索最后插入的 ID 或序列值,请参阅 PDO get the last ID inserted