How to use yii framework builder, update, and delete_PHP tutorial

WBOY
Release: 2016-07-13 10:31:34
Original
925 people have browsed it

The query builder that comes with Yii is very easy to use. It saves the process of spelling sql. I encountered such a problem when writing a statement today

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

Use $operate_rst to record the operation results. There is no problem when executing a new insert. However, when updating, sometimes it will show that the operation failed. After checking for a long time, I can't find the reason, so I have to read the document

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

When you see the return item, it is

Copy code The code is as follows:

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

I immediately understood the problem, because sometimes the data may not be changed but the update operation is triggered, so the number of rows changed at this time is 0, and the returned judgment will enter the error code. .

Similarly, 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 the return value. A value of 0 may also indicate that the DB operation was successful.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/763015.htmlTechArticleThe query builder that comes with Yii is still very easy to use. It saves the process of spelling sql. I am writing one today. When encountering such a problem when making statements, copy the code as follows: $connection = Yii::app...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!