thinkphp if data is not modified when data is updated, the result is wrong to be returned

Source: Internet
Author: User

in Thinkphp's curd, a strange problem arises when using the Save method, that is, if the data is not updated (same as the original data), the return value is judged to be false. It was a long time ago that the problem was discovered, once thought to be an official code problem, but it has been delayed until recently to solve the problem. I have to say, "Tomorrow will be a day, so much tomorrow." Everything to-morrow, everything is wasted. ", with June Mutual encouragement!     after searching for the problem on the official website, it was found that the authorities had solved the problem, the original judgment:
$result = $Model->where (' id=1 ')->save ($data), if ($result) {    echo ' success ';} Else{    echo ' false ';}



Workaround: Use "= = =" To determine the return value
$result = $Model->where (' id=1 ')->save ($data), if ($result!== false) {    echo ' success ';} Else{    echo ' false ';}



Problem solved, but still did not understand the reason, so went to see the Save method to implement the source code (3.2.3 version MySQL driver):



The bottom of the model in TP is encapsulated by the PDO class, so continue looking for the rowcount method in PDO:



The reason is found, because the data is not changed, so the number of bars affected by the data is zero, the return value is 0, and 0 in the PHP judgment is false, so there is the above situation. When using "= = =" Judgment, because the variable type is judged, 0 and false are not equal, the problem is solved.

thinkphp if data is not modified when data is updated, the result is wrong to be returned

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.