Gayner Posted December 13, 2009 Share Posted December 13, 2009 Like Newest user: _____ How do i Pull the newest user in my table, i know how to pull mysql but do i just LIMIT 1? lol Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/ Share on other sites More sharing options...
oni-kun Posted December 13, 2009 Share Posted December 13, 2009 $query = mysql_query('SELECT Name FROM Table WHERE ID = (SELECT count(*) FROM Table)'); That should work to get last ID, you can echo the name of it. Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/#findComment-976327 Share on other sites More sharing options...
ngreenwood6 Posted December 13, 2009 Share Posted December 13, 2009 if you are using an autoincrement id you could do: $query = "SELECT * FROM users ORDER BY id DESC LIMIT 1"; The reason I did that is by doing it DESC you will get the last user and it will only return 1 result that way it doesnt go through all the users. Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/#findComment-976328 Share on other sites More sharing options...
ngreenwood6 Posted December 13, 2009 Share Posted December 13, 2009 please do not use oni-kuns method and I will tell you why. Say you have 20 users in the database but maybe some of the other users were deleted leaving there id blank this would fail. The reason is say you had 25 registered but 5 were removed. the last id would be 25. Now if you used the method that oni-kun suggests it would select the count (or 20 in this case) as the id which would not be the correct id. Also that id might not even exist if it was one that was deleted. Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/#findComment-976329 Share on other sites More sharing options...
Gayner Posted December 13, 2009 Author Share Posted December 13, 2009 if you are using an autoincrement id you could do: $query = "SELECT * FROM users ORDER BY id DESC LIMIT 1"; The reason I did that is by doing it DESC you will get the last user and it will only return 1 result that way it doesnt go through all the users. sweet thank u Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/#findComment-976331 Share on other sites More sharing options...
GFXUniverse Posted December 13, 2009 Share Posted December 13, 2009 yes limit 1 as ngreenwood6 said here is fully explained <?php $query = "SELECT * FROM users ORDER BY id DESC LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($result); $newest_member = $row['username']; ?> Welcome to our newest member <?php echo $$newest_member; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184945-how-do-i-do-last-user-from-mysql-table/#findComment-976334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.