Jump to content

Get the UPDATED row


phppup
Go to solution Solved by kicken,

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.