3 Years
Discussion Span
Last Post by phoenix254

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.

Edited by phoenix254


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.