简体   繁体   English

获取最后一个ID PDO

[英]Getting last id PDO

This is what i have: 这就是我所拥有的:

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

echo $id;

So, i want result to be for example "There is $id registered users". 因此,我希望结果是例如“有$ id个注册用户”。

You never need to select the "last id" from a table. 您无需从表中选择“最后一个ID”。 Every time you think you you need it, you need something else. 每当您认为自己需要时,就需要其他东西。 For example, whatever "Last id" has nothing to do with the number of users registered. 例如,任何“ Last id”都与注册的用户数量无关。 Instead, you have to count users: 相反,您必须计算用户:

$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. PDO::lastInsertId返回最后插入的行或序列值的ID。
https://secure.php.net/manual/en/pdo.lastinsertid.php . https://secure.php.net/manual/zh/pdo.lastinsertid.php

What you want is a count of registered users, see Row count with PDO . 你想要的是注册用户的数量 ,见行与PDO计数

To retrieve the last inserted ID or sequence value see PDO get the last ID inserted . 要检索最后插入的ID或序列值,请参见PDO获取最后插入的ID

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

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