Jump to content

Get ID of previous "update" query


EchoFool

Recommended Posts

Just run another query directly afterwards.. SELECTing the most recent addition. 

 

/*bllahal blah blha blah 
  UPDATE query.. with PHP           *////////
$lastID = mysql_result(   mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 1"),  0   );
echo  "The last updated id is " . $lastID;

Link to comment
Share on other sites

@zeodragonzord and @Zanus

 

Under modern interrupt driven multitasking operating systems (i.e. current ones  :D ) and also in situations where one or more servers are making use of a separate database server, other quires due to concurrent visitors can occur at any time and you cannot guarantee that executing a separate query to get the highest value will result in the value that was just operated on by the current invocation of a php script. This is why things like the mysql_insert_id() are maintained per connection so that they are accurate for the current visitor regardless of what other queries are being executed.

 

JustLikeIcarus already suggest the best solution for a deliberate UPDATE query. You just updated a specific record and you know which one because you know the WHERE condition that matched that record. You can also put a SET id=LAST_INSERT_ID(id) in the UPDATE query to cause the mysql_insert_id() to be set with the value of the id column that was just updated.

 

If the UPDATE query operated on more than one row, things like mysql_insert_id() only return the first row affected and you would need to individually update each affected row or SET a column to some unique flag value (time values are not unique) that was generated by the php script in order to be able to identify all the affected rows.

Link to comment
Share on other sites

Knowing what you will update before you the update is a good idea since you can assure that the rows in focus are already in the database.  If you do a SELECT beforehand, you can verify that rows exist and keep a copy of the primary keys.  Run your update statement and you'll still have your primary key list of rows you've updated.

 

If you do a straight UPDATE, you can't guarantee that you've affected any rows, if criteria doesn't match any rows.

Link to comment
Share on other sites

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.