PDO-& gt; exec () fails to be modified if the data is not changed. PDO-> exec () fails to be modified if the data is not changed. The returned value is 0, and I use the 0 value to determine whether the data is modified. but the problem arises. when I use transactions to process multiple tables, some tables modify the data, some tables are not modified. If the data in one of the tables is not modified, it will lead to the failure of the entire transaction and throw an exception statement, resulting in a rollback modification failure. Is there any way to solve this problem?
Try {$ DB-> BeginTransaction (); // transaction start $ updateNews = $ DB-> Update (DBI ('prefix '). 'news', $ arrNews, 'id', $ ID); if ($ updateNews! = FALSE) {echo'
The news Title table is modified successfully. ';} Else {throw new PDOException (' exception: an error occurred while modifying the news Title table. '); // Throw an exception} $ updateConnent = $ DB-> Update (DBI ('prefix '). 'newsconnent', $ arrConnent, 'newsid ', $ ID); if ($ updateConnent! = FALSE) {echo'
The news content table is modified successfully. ';} Else {throw new PDOException (' exception: an error occurred while modifying the news content table. '); // Throw an exception} $ DB-> Commit (); // success, submit .} Catch (PDOException $ e) {echo 'edit failed: '. $ e-> getMessage (); $ DB-> Rollback (); // failed, Rollback}
Reply to discussion (solution)
PDO: exec (): the reason why 0 is returned when update is executed is:
1. there is no record with the specified condition
2. the content of the modified field is the same as that of the field to be modified.
The reason why PDO: exec () returns false is that the SQL command is incorrect.
I don't know how your $ DB-> Update was written. Why does it determine $ updateNews! = FALSE
The code you give is to modify the title and content at the same time, but in fact this rarely happens.
PDO: exec (): the reason why 0 is returned when update is executed is:
1. there is no record with the specified condition
2. the content of the modified field is the same as that of the field to be modified.
The reason why PDO: exec () returns false is that the SQL command is incorrect.
I don't know how your $ DB-> Update was written. Why does it determine $ updateNews! = FALSE
The code you give is to modify the title and content at the same time, but in fact this rarely happens.
PDO: exec (): the reason why 0 is returned when update is executed is:
1. there is no record with the specified condition
2. the content of the modified field is the same as that of the field to be modified.
The reason why PDO: exec () returns false is that the SQL command is incorrect.
I don't know how your $ DB-> Update was written. Why does it determine $ updateNews! = FALSE
The code you give is to modify the title and content at the same time, but in fact this rarely happens.
The content and title are table sharding. This does not seem to have been the case with mysqli. This time I want to use transactions to store and modify two tables.
When exec () returns 0 in the encapsulated $ DB-> Update () method, it is considered that the modification failed, and $ DB-> Update () returns FALSE, otherwise, the number of affected items is returned. This does not seem to be a problem when processing a single table, because the data table must be modified, but as a transaction, it is found that if one of the tables is not modified, that is, 0 is returned, the encapsulated method will be regarded as failed to be modified, and an exception will be thrown to rollback. Shouldn't transactions be used in this case? However, I think this problem will not be solved, and it seems that it will be a problem in the future. There must be occasional similarities between the data to be modified and the source data.
Currently, you can solve this problem by adding a time field in the content table and the title table. this LastTime time is updated every time you store it. This solves the storage failure problem, but does not know whether there is a better way in the program.
The modification time is correct and conforms to the business process.
However, modifying only the title or only the content often happens.
In fact, the title and content are not strongly related (actually, the id is used to establish a connection)
Therefore, you do not need to use transactions for modification. you need to use transactions for insertion and deletion.
The modification time is correct and conforms to the business process.
However, modifying only the title or only the content often happens.
In fact, the title and content are not strongly related (actually, the id is used to establish a connection)
Therefore, you do not need to use transactions for modification. you need to use transactions for insertion and deletion.
Thank you very much.