geekisthenewsexy Posted October 8, 2010 Share Posted October 8, 2010 hi there, so okay i have a table named course which has 2 fields. c_id and c_name c_id here isn't unique but is indexed. okay,my problem is how to get the last inserted row in this table right after a record has been inserted. i was thinking i'd do a little query and then echo the results back to where the user fills up a form with c_id and c_name fields.. so is there a way to do this?like getting all values from the last inserted row? Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/ Share on other sites More sharing options...
trq Posted October 8, 2010 Share Posted October 8, 2010 mysql_insert_id Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/#findComment-1120134 Share on other sites More sharing options...
Adam Posted October 8, 2010 Share Posted October 8, 2010 That won't work here given that the "c_id" field isn't an auto incremented field. Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/#findComment-1120135 Share on other sites More sharing options...
Pawn Posted October 8, 2010 Share Posted October 8, 2010 Bit of a hack, but... Add a table column "c_created", type TIMESTAMP, default CURRENT_TIMESTAMP. Select as follows: $sql = "SELECT * FROM courses ORDER BY c_created DESC LIMIT 1"; Of course, if two rows are inserted in the same second you have a problem. Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/#findComment-1120155 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2010 Share Posted October 8, 2010 If you want to get the id of the row that was just inserted by the current visitor in your script, you need to add an auto-increment field to your table. Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/#findComment-1120156 Share on other sites More sharing options...
geekisthenewsexy Posted October 11, 2010 Author Share Posted October 11, 2010 hey guys, thanks for helping.. i have added an auto-increment on c_id field but i didn't make it unique.. iv'e used this one mysql_insert_id(). thanks! Quote Link to comment https://forums.phpfreaks.com/topic/215400-how-to-get-last-inserted-row/#findComment-1121005 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.