Pdo->exec () The modification fails if the data is not changed when the data is modified.

Source: Internet
Author: User
Pdo->exec () The modification fails if the data is not changed when the data is modified. The return is 0, and I use the change back to determine whether the data is modified in the work, but the problem comes, when I use transactions with multiple tables, some tables will modify the data, and some tables will not be modified. If the data of one of the tables is not altered, it will lead to the entire transaction failure and execute the throw exception statement, resulting in rollback modification failure. Is there any way to solve this problem?

try {$DB->begintransaction ();//transaction begins $updatenews= $DB->update (DBI (' Prefix '). ' News ', $arrNews, ' ID ', $ID); if ($updateNews!==false) {echo '
The News title table was modified successfully. ';} Else{throw New Pdoexception (' exception: the News title table modification failed. ')///Throw exception} $updateConnent = $DB->update (DBI (' Prefix '). ' Newsconnent ', $arrConnent, ' NewsID ', $ID); if ($updateConnent!==false) {echo '
The News content table was modified successfully. ';} Else{throw New Pdoexception (' exception: the News content table modification failed. ');//Throw exception} $DB->commit ();//success, commit. } catch (Pdoexception $e) {echo ' Edit failed: '. $e->getmessage (); $DB->rollback ();//failed, rollback}


Reply to discussion (solution)

Pdo::exec () The reason for returning 0 when performing an update is:
1, there is no record of the specified conditions
2. The content of the modified field is the same as the content to be modified
Pdo::exec () returns false because of an incorrect SQL instruction

Do not know how your $DB->update is written, why will judge $updateNews!==false

The code you're giving is changing the title and the content at the same time, but in fact this rarely happens.

Pdo::exec () The reason for returning 0 when performing an update is:
1, there is no record of the specified conditions
2. The content of the modified field is the same as the content to be modified
Pdo::exec () returns false because of an incorrect SQL instruction

Do not know how your $DB->update is written, why will judge $updateNews!==false

The code you're giving is changing the title and the content at the same time, but in fact this rarely happens.



Pdo::exec () The reason for returning 0 when performing an update is:
1, there is no record of the specified conditions
2. The content of the modified field is the same as the content to be modified
Pdo::exec () returns false because of an incorrect SQL instruction

Do not know how your $DB->update is written, why will judge $updateNews!==false

The code you're giving is changing the title and the content at the same time, but in fact this rarely happens.



The contents and headings are divided into tables, and there seems to be no such problem with mysqli. This time I want to modify two tables with transactions to handle storage.
In the encapsulated $db->update () method, when exec () returns 0, it is considered a modification failure, $DB->update () returns false, otherwise the number of affected bars is returned. This doesn't seem to be a problem when working with a single table, because the data table is definitely going to be modified, but as a transaction, I find that if one of the tables is not modified, that is, it returns 0, the method that I encapsulate is considered to be a modification failure and throws an exception to rollback. Is this the case that the business should not be used? But I think this problem is not solved, it seems to be a problem later. It is certain that the data to be modified will occasionally appear the same as the source data.
At present, with the stupid method can be solved, that is, in the table of contents and the title table are added a Time field, each time the storage will update the lasttime time. It also solves the problem of storage failure, but I don't know if there is a better way to do it.

Plus the modification time is right, in line with the business process
However, it often happens that only the title is modified or only the content is modified.
In fact, the title and content are not strongly linked (it is actually used to establish contact with the ID)
So the modification does not require transactions, inserts and deletes need to use transactions

Plus the modification time is right, in line with the business process
However, it often happens that only the title is modified or only the content is modified.
In fact, the title and content are not strongly linked (it is actually used to establish contact with the ID)
So the modification does not require transactions, inserts and deletes need to use transactions



Thank you, thank you very much.
  • 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.