Yii's own query Builder is easy to use, eliminating the process of spelling SQL. When writing a statement, you encounter such a problem:
- $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 ();
Copy CodeWith $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. |