If no data is modified when thinkphp updates the data, an error is returned. thinkphp returns

Source: Internet
Author: User

If no data is modified when thinkphp updates the data, an error is returned. thinkphp returns
In the CURD of thinkphp, a strange problem occurs when the save method is used, that is, if the data is not updated (the same as the original data), the return value is false. In fact, this problem was discovered a long time ago. I thought it was a problem with the official code, but it had been delayed until recently before I thought it was necessary to solve this problem. I have to say, "Tomorrow, tomorrow, how many things are there tomorrow. Everything is waiting for tomorrow, and everything is always ready .", Share with you! After searching for the problem on the official website, I found that the problem has been solved by the official website. The original judgment was as follows:

$ Result = $ Model-> where ('Id = 1')-> save ($ data); if ($ result) {echo 'success ';} else {echo 'false ';}



Solution: Use "=" to determine the return value.
$ Result = $ Model-> where ('Id = 1')-> save ($ data); if ($ result! = False) {echo 'success';} else {echo 'false ';}



The problem was solved, but I still did not understand the cause. So I checked the source code of the save method (mysql driver 3.2.3 ):



The underlying layer of the model in Tp is encapsulated by the pdo class, so we can continue to look for the rowCount method in the pdo:



The cause is found. Because the data has not changed, the number of affected items in the data is zero, and the return value is 0, while 0 is false in php judgment, so the above situation occurs. When "=" is used to determine the type of the variable, the values 0 and false are not equal. The problem is solved.

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.