This article describes how to use the yii framework builder, update, and delete. For more information, see
This article describes how to use the yii framework builder, update, and delete. For more information, see
The query builder provided by Yii is still very useful, saving the SQL spelling process. Today, when I write a statement, I encountered such a problem.
The Code is 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 ();
$ Operate_rst is used to record the operation results. It is no problem to execute the new insert statement. However, during the update process, the operation may fail to be displayed. After checking for half a day, the cause cannot be found, so I have to go through the document.
# Update-detail
Which of the following statements about return is:
The Code is as follows:
{Return} integer number of rows affected by the execution.
I understand the problem in an instant, because sometimes the data is not changed but the update operation is triggered. Therefore, when the number of rows being changed is 0, the returned judgment enters the error code ..
Similarly, the return values of the delete () and insert () methods also indicate the number of affected rows. Therefore, the delete and insert methods can determine whether the operation is successful based on whether the return values are greater than 0, however, the update operation is not necessarily successful. If the returned value is 0, the database operation may be successful.