Home > Backend Development > PHP Tutorial > Yii framework builder, update, delete usage examples

Yii framework builder, update, delete usage examples

WBOY
Release: 2016-07-25 09:12:52
Original
1050 people have browsed it

The query builder that comes with Yii is very easy to use and saves you the process of spelling out SQL.

I encountered this problem when writing a statement:

  1. $connection = Yii::app()->db;
  2. $command = $connection->createCommand();
  3. $operate_rst = 0;
  4. if(!empty($_POST['lid '])){
  5. $operate_rst = $command->update('emg_landing', $landing_info, 'lid=:lid', array(':lid' => $_POST['lid']));
  6. }
  7. else{
  8. $operate_rst = $command->insert('emg_landing', $landing_info);
  9. }
  10. $connection->active = false;
  11. if($operate_rst > 0){
  12. Functions::returnOk ('OK!');
  13. }
  14. Functions::returnErrorJson();
Copy code

Use $operate_rst to record the operation results. There is no problem in executing the new insert, but when updating, sometimes it will show that the operation failed. , I checked for a long time and couldn’t find the reason, so I had to look through the documents. http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail See the return item: {return} integer number of rows affected by the execution. Sometimes the data may not be changed but an update operation is triggered, so the number of changed rows at this time is 0, and the returned judgment will enter the error code. In the same way, the meaning of the return value of delete() and insert() methods is also the number of affected rows, so delete and insert can judge whether the operation is successful based on whether the return value is greater than 0, but the update operation is not necessarily, and the return value is 0. It may indicate that the DB operation was successful.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template