phppup Posted September 18 Share Posted September 18 As a follow-up to my recent post to determining the AFFECTED ROWS after an update to a table, I am now interested in obtaining the row's ID. I previously obtained HOW MANY rows were affected. Now I want to know WHICH ROWS. In actuality, I'm using LIMIT 1 in my update, so I'm looking for a PHP echo of the row ID (so that I can refer back to it). I've seen some examples in SQL that involve a psuedo-INSERT, but they don't offer a PHP transition (procedurally) to accomplish an echo confirmation. Guidance or code solutions appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/324116-get-the-updated-row/ Share on other sites More sharing options...
mac_gyver Posted September 18 Share Posted September 18 you can use the MySql LAST_INSERT_ID(expr) function in an insert/update query to make the expr value present in that query accessible following the execution of the query. expr can be anything, such as a column (which would be the id column for what you are asking. i typically use it in an insert ... on duplicate key update ... query to tell if the update part was executed by getting the id of the row that was updated), the result of a calculation (such as incrementing and updating a page count column and accessing the resulting count), or the result of a comparison (i've got an example in my project archive where it is being used to return the true/false expr1 value from an IF(expr1,expr2,expr3) statement in a query) . you can then access the resulting value using the last insert id method/property for the php database extension you are using. Quote Link to comment https://forums.phpfreaks.com/topic/324116-get-the-updated-row/#findComment-1635467 Share on other sites More sharing options...
phppup Posted September 18 Author Share Posted September 18 @mac_gyver I've gathered most of the "it can be done" from my web searches. I need an example (preferably in procedural PHP) of HOW to do it. I have made a few attempts, but none are producing results. Quote Link to comment https://forums.phpfreaks.com/topic/324116-get-the-updated-row/#findComment-1635468 Share on other sites More sharing options...
phppup Posted September 19 Author Share Posted September 19 I've tried a few iterations but cannot get the correct syntax $sql = "UPDATE $table SET item=? WHERE id<10 id = LAST_INSERT_ID() ORDER BY id ASC LIMIT 1; SELECT LAST_INSERT_ID();"; //and then $last_id = mysqli_insert_id($conn); echo "Last inserted ID is: " . $last_id; Where is my mistake? Quote Link to comment https://forums.phpfreaks.com/topic/324116-get-the-updated-row/#findComment-1635514 Share on other sites More sharing options...
Solution kicken Posted September 19 Solution Share Posted September 19 There is an example near the end of the documentation for last_insert_id. Something like this I am guessing. $sql = " UPDATE $table SET item=?, id=LAST_INSERT_ID(id) WHERE id<10 ORDER BY id ASC LIMIT 1; "; //and then $last_id = mysqli_insert_id($conn); echo "Last inserted ID is: " . $last_id; 1 Quote Link to comment https://forums.phpfreaks.com/topic/324116-get-the-updated-row/#findComment-1635516 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.