jocorok Posted November 5, 2009 Share Posted November 5, 2009 One way is to do it like this: $result = mysql_query("INSERT INTO table..."); $lastId = mysql_insert_id(); $result = mysql_query("SELECT * FROM table where ID = $lastId"); Seems pretty dumb to make a new query, to get the data you just inserted. Of course I already have the data I was inserting, but I reckon getting it as some sort of return from mysql is safer or less error prone. Is there a function like mysql_insert_id() to get the latest inserted data, besides id? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/ Share on other sites More sharing options...
trq Posted November 5, 2009 Share Posted November 5, 2009 Is there a function like mysql_insert_id() to get the latest inserted data, besides id? No. Nothing to stop you writing your own though. Quote Link to comment https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/#findComment-951640 Share on other sites More sharing options...
jocorok Posted November 5, 2009 Author Share Posted November 5, 2009 Thanks. Hehe, I'd love to do the function, if I knew how. I like to have my web apps optimized and userfriendly as possible. I update tables using ajax, and I'd like to update the page usinf js and DOM, without refreshing the page. Which is no problem, just thought i'd make it safer. How is this usually done? Just implement the data that was sent to the server, instead of getting the actual data that was stored in db? thx Quote Link to comment https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/#findComment-951699 Share on other sites More sharing options...
Mchl Posted November 5, 2009 Share Posted November 5, 2009 As long as mysql_query returns true for INSERT, you can be pretty sure1 that data got inserted into a table. If you need to be 100% sure that what you display is what has been stored in db, then you have to SELECT this data. Otherwise just display what you've just send. 1: That's assuming there are no ON INSERT triggers or MySQL Proxy in between that do some data filtering. Quote Link to comment https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/#findComment-951706 Share on other sites More sharing options...
jocorok Posted November 5, 2009 Author Share Posted November 5, 2009 Thanks. That's how I was doing it, allays thinking it was a bit "hackish". Quote Link to comment https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/#findComment-951742 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.