i want to know how i can get last user id from my database ?

Recommended Answers

All 6 Replies

Member Avatar

THis depends entirely on what you mean by last user_id. Do you mean the one with the largest value or the one that was inserted last?

Usually these will be the same thing if we're talking about user_id as being the Primary Key which is autoincremented.

So you have a choice:

SELECT MAX(user_id) FROM users


SELECT user_id FROM users ORDER BY user_id DESC LIMIT 1

If you want to get the last id from the previous INSERT via PDO, you can do this:


or you could use:


But I've never tried it, so can't say.

SELECT user_id FROM users ORDER BY user_id DESC LIMIT 1
its working for php

here is my php code

$lastid=mysqli_query($con,"SELECT user_id FROM login ORDER BY user_id DESC LIMIT 1") or die("no work");
    echo $lastidoftable;

its working perfectly when url dont ' syntax. whem i put in url tht syntax it generate some errors and now i want to convert it in pdo nut i dont know how is possible.

please sir help me

Member Avatar

Couple of things.

You're not using "last insert id" - this has a different use to the SQL you're using. Make sure you're using the right approach. Only you will know this.

If you're using the SQL method, then you do not need a 'while loop' as you're only retrieving a single record.

Anyhow, you say you want to use PDO, then you start using mysqli. Make up your mind. This is all a bit confused. Come back when you have this straight in your head and state EXACTLY what you need.

ok thanks understand..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.