Yii Framework Builder, UPDATE, delete usage example

Source: Internet
Author: User

Yii's own query Builder is easy to use, eliminating the process of spelling SQL.

When writing a statement, you encounter such a problem:

    1. $connection = Yii::app ()->db;
    2. $command = $connection->createcommand ();
    3. $operate _rst = 0;
    4. if (!empty ($_post[' lid ')) {
    5. $operate _rst = $command->update (' emg_landing ', $landing _info, ' lid=:lid ', Array (': lid ' = ' $_post[' lid ']));
    6. }
    7. else{
    8. $operate _rst = $command->insert (' emg_landing ', $landing _info);
    9. }
    10. $connection->active = false;
    11. if ($operate _rst > 0) {
    12. Functions::returnok (' ok! ');
    13. }
    14. Functions::returnerrorjson ();
Copy Code

With $operate _rst to record the results of the operation, the implementation of the new insert is not a problem, but in the update, sometimes show the operation failed, check for half a day, and can not find the reason, had to go through the document http://www.yiiframework.com/doc/api/ 1.1/cdbcommand#update-detail See return: {return} integer number of rows affected by the Execution. Sometimes the data is not changed but the update operation is triggered, so the number of rows changed at this time is 0, and the returned judgment goes into the error code. Similarly, the method return value of Delete () and insert () is also affected by the number of rows, so delete and insert can determine whether the operation succeeds based on whether the return value is greater than zero, but the update operation is not necessarily, and a return value of 0 may indicate a successful operation on the DB.

  • 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.