Yii Framework Builder, UPDATE, delete How to use _php tutorial

Source: Internet
Author: User
Yii's own query Builder is still very useful, eliminating the process of spelling SQL, today when writing a statement encountered such a problem

Copy the Code code as follows:
$connection = Yii::app ()->db;
$command = $connection->createcommand ();
$operate _rst = 0;
if (!empty ($_post[' lid ')) {
$operate _rst = $command->update (' emg_landing ', $landing _info, ' lid=:lid ', Array (': lid ' = ' $_post[' lid ']));
}
else{
$operate _rst = $command->insert (' emg_landing ', $landing _info);
}
$connection->active = false;
if ($operate _rst > 0) {
Functions::returnok (' ok! ');
}
Functions::returnerrorjson ();

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 that one item is

Copy the Code code as follows:
{return} integer number of rows affected by the execution.

Immediately understand the problem, because sometimes may not change the data but triggered the update operation, so at this time the number of rows changed is 0, the return of the judgment entered 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.

http://www.bkjia.com/PHPjc/763015.html www.bkjia.com true http://www.bkjia.com/PHPjc/763015.html techarticle Yii comes with the query Builder is still very useful, eliminating the process of spelling SQL, today when writing a statement encountered such a problem copy code code as follows: $connection = Yii::app ...

  • Related Article

    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.