How does MySQL confirm that the update, delete, and insert statements are successfully executed?

Source: Internet
Author: User
How does MySQL confirm that the update, delete, and insert statements are successfully executed? For example, the question is always determined by mysql_affected_rows (). In this way, almost every update, delete, and insert statement must be followed.
If (mysql_affected_rows ()! = 1)
{
Error
}

Is there any easier way?


Reply to discussion (solution)

Mysql_affected_rows is not used to check whether the execution is successful.
It is used to observe how many records are operated by related SQL commands.

Direct

If (execute SQL statement) {...} else {......}

Mysql_affected_rows is not used to check whether the execution is successful.
It is used to observe how many records are operated by related SQL commands.


It is my statement. I just want to judge whether the insert, delete, and update statements have successfully affected at least one row.

It must be executed to determine which rows of mysql_affected_rows () are affected.

No matter how many rows (including 0 rows) are affected, it is not an error.

No matter how many rows (including 0 rows) are affected, it is not an error.


Of course I know this...

For example, the question is always determined by mysql_affected_rows (). In this way, almost every update, delete, and insert statement must be followed.
If (mysql_affected_rows ()! = 1)
{
Error
}

Is there any easier way?



In addition, I want to determine that the update, delete, and insert statements affect at least one row. In most cases, such judgment logic is required.
This problem occurs even if the update, delete, and insert operations affect zero rows and the transaction will not be rolled back.

Update will not be modified if there is no matching record. if the modified value is the same as the original value, the modification will not actually happen.
If delete does not have a qualified record, it will not be deleted. if there are multiple qualified records, they will naturally be deleted together.
Only one insert statement is affected, even if 100 insert statements are written in a scaling statement.

I can't see how it relates to transactions.

Update will not be modified if there is no matching record. if the modified value is the same as the original value, the modification will not actually happen.
If delete does not have a qualified record, it will not be deleted. if there are multiple qualified records, they will naturally be deleted together.
Only one insert statement is affected, even if 100 insert statements are written in a scaling statement.

I can't see how it relates to transactions.



After thinking about it, what you said makes sense.
Let me give an example of my question:
For example, if you want to use virtual currency to buy virtual items, go to update... set money =... where user_id =... after this statement is executed, do you need mysql_affected_rows () to verify that one row is successfully updated? I was worried that I didn't actually update to the money field. then I executed the following service to add virtual items to the user.
If select money from... for update is executed before, is it safe?


Update will not be modified if there is no matching record. if the modified value is the same as the original value, the modification will not actually happen.
If delete does not have a qualified record, it will not be deleted. if there are multiple qualified records, they will naturally be deleted together.
Only one insert statement is affected, even if 100 insert statements are written in a scaling statement.

I can't see how it relates to transactions.



After thinking about it, what you said makes sense.
Let me give an example of my question:
For example, if you want to use virtual currency to buy virtual items, go to update... set money =... where user_id =... after this statement is executed, do you need mysql_affected_rows () to verify that one row is successfully updated? I was worried that I didn't actually update to the money field. then I executed the following service to add virtual items to the user.
If select money from... for update is executed before, is it safe?


Direct if (update operation...) {purchase successful...} else {purchase failed...} is not good. if update operation fails, false is returned.

$result = mysql_query("update ……")if ($result !== false){    echo "exec success";}else{    echo "exec error:".mysql_error():}



Update will not be modified if there is no matching record. if the modified value is the same as the original value, the modification will not actually happen.
If delete does not have a qualified record, it will not be deleted. if there are multiple qualified records, they will naturally be deleted together.
Only one insert statement is affected, even if 100 insert statements are written in a scaling statement.

I can't see how it relates to transactions.



After thinking about it, what you said makes sense.
Let me give an example of my question:
For example, if you want to use virtual currency to buy virtual items, go to update... set money =... where user_id =... after this statement is executed, do you need mysql_affected_rows () to verify that one row is successfully updated? I was worried that I didn't actually update to the money field. then I executed the following service to add virtual items to the user.
If select money from... for update is executed before, is it safe?


Direct if (update operation...) {purchase successful...} else {purchase failed...} is not good. if update operation fails, false is returned.



Okay. I decided to give it a try. it looks more concise than mysql_affected_rows. I don't know if there are any nuances in the effect.

$result = mysql_query("update ……")if ($result !== false){    echo "exec success";}else{    echo "exec error:".mysql_error():}



Same as above. thank you for trying it out.

If you are not sure that update... set money = money-1 will actually be executed
So do you need to select again to confirm that money is reduced by 1, instead of 100?

$ Result = mysql_query ("update ...... ")
False is returned only when the SQL command is incorrect.
So you don't need to try it.

If you are not sure that update... set money = money-1 will actually be executed
So do you need to select again to confirm that money is reduced by 1, instead of 100?

$ Result = mysql_query ("update ...... ")
False is returned only when the SQL command is incorrect.
So you don't need to try it.

If you are not sure that update... set money = money-1 will actually be executed
So do you need to select again to confirm that money is reduced by 1, instead of 100?

$ Result = mysql_query ("update ...... ")
False is returned only when the SQL command is incorrect.
So you don't need to try it.


Okay. it sounds reliable. thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.