Yii Framework Builder, UPDATE, delete use method _php instance

Source: Internet
Author: User
Tags yii

Yii with the Query Builder is still very useful, save the process of the spelling of SQL, today in writing a statement of the time encountered such a problem

Copy 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 ();

Use $operate _rst to record the results of the operation, the implementation of the new insert no problem, but at the time of the update, sometimes show the operation failed, check for a half-day, also can not find the reason, had to go to the document

Http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail

See return that one is

Copy Code code as follows:

{return} An integer number of rows affected by the execution.

Instant understanding of the problem, because sometimes there may not be changes to the data but triggered the update operation, so this time the changed number of rows is 0, the return of the judgment is entered into the error code.

Similarly, the Delete () and insert () methods return value meaning is also the number of rows affected, so delete and insert can determine whether the operation succeeded based on whether the return value is greater than 0来, but the update operation does not necessarily mean that the return value of 0 may also indicate success for the DB operation.

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.