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

Recommended Answers

All 6 Replies

Member Avatar for diafol

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

or

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:

http://php.net/manual/en/pdo.lastinsertid.php

or you could use:

SELECT LAST_INSERT_ID()

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

thanks
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");
while($row=mysqli_fetch_assoc($lastid))
    {
    $lastidoftable=$row['user_id'];
    }
    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 for diafol

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, networking, learning, and sharing knowledge.