0

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".

Monk
  • 105
  • 9

2 Answers2

2

You never need to select the "last id" from a table. 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. Instead, you have to count users:

$count = $handler->query('SELECT count(id) FROM users')->fetchColumn();
echo "There is $count registered users";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • `SELECT id FROM users ORDER BY id DESC LIMIT 1` This is also working but maybe it's not the right one, thank you. – Monk Sep 09 '16 at 06:35
0

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

What you want is a count of registered users, see Row count with PDO.

To retrieve the last inserted ID or sequence value see PDO get the last ID inserted.

Community
  • 1
  • 1
Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
  • He don't need the count of *results*. Simply because he don't have them. – Your Common Sense Sep 09 '16 at 06:56
  • The word "results" can be used if you selected some data, got some results and then want to count them. While the OP don't have any query results. the only result he have is the count itself and obviously there is no point in counting it. – Your Common Sense Sep 09 '16 at 07:29