How can I get ID of the last updated row in MySQL

0 votes
I updated the row of my mysql database so next time how can i get id of last update row?

Thanxx in advanced!!
Apr 9, 2020 in PHP by kartik
• 37,490 points
2,239 views

1 answer to this question.

0 votes

Hello @kartik,

Suppose, item_id is an integer identity column in items table and you update rows with the following statement:

UPDATE items
SET qwe = 'qwe'
WHERE asd = 'asd';

Then, to know the latest affected row right after the statement, you should slightly update the statement into the following:

UPDATE items
SET qwe = 'qwe',
    item_id=LAST_INSERT_ID(item_id)
WHERE asd = 'asd';
SELECT LAST_INSERT_ID();

If you need to update only really changed row, you would need to add a conditional update of the item_id through the LAST_INSERT_ID checking if the data is going to change in the row.

Thank You!!

answered Apr 9, 2020 by Niroj
• 82,540 points

Related Questions In PHP

0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,540 points
2,312 views
0 votes
1 answer

How to get a list of user accounts using the command line in MySQL?

Hello @kartik, Use this query: SELECT User FROM mysql.user; Which ...READ MORE

answered Aug 17, 2020 in PHP by Niroj
• 82,540 points
83 views
0 votes
1 answer

How do I get the HTML code of a web page in PHP?

Hello @kartik, If your PHP server allows url ...READ MORE

answered Oct 6, 2020 in PHP by Niroj
• 82,540 points
103 views
0 votes
1 answer

How can I get enum possible values in a MySQL database?

Hello @kartik, I have a codeigniter version for ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,540 points
104 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,540 points
4,797 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,540 points
542 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,540 points
408 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,540 points
5,417 views
0 votes
1 answer

How do I get the last inserted ID of a MySQL table in PHP?

Hello @kartik, If you're using PDO, use PDO::lastInsertId. If you're ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,540 points
125 views
0 votes
1 answer

How can I get the session ID in Laravel?

Hii, Use: $session_id = Session::getId(); or $session_id = ses ...READ MORE

answered Nov 18, 2020 in PHP by Niroj
• 82,540 points
168 views