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