Jump to content

[SOLVED] INSERT - get last inserted data


Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/180388-solved-insert-get-last-inserted-data/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.