Home > Backend Development > PHP Tutorial > Yii Query Builder insert(), update(), delete() usage_PHP tutorial

Yii Query Builder insert(), update(), delete() usage_PHP tutorial

WBOY
Release: 2016-07-13 10:32:46
Original
798 people have browsed it

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

<span $connection</span> = Yii::app()-><span db;
</span><span $command</span> = <span $connection</span>-><span createCommand();

</span><span $operate_rst</span> = 0<span ;
</span><span if</span>(!<span empty</span>(<span $_POST</span>['lid'<span ])){
    </span><span $operate_rst</span> = <span $command</span>->update('emg_landing', <span $landing_info</span>, 'lid=:lid', <span array</span>(':lid' => <span $_POST</span>['lid'<span ]));
}
</span><span else</span><span {
    </span><span $operate_rst</span> = <span $command</span>->insert('emg_landing', <span $landing_info</span><span );
}
</span><span $connection</span>->active = <span false</span><span ;
</span><span if</span>(<span $operate_rst</span> > 0<span ){
    Functions</span>::returnOk('OK!'<span );
}<br />Functions::returnErrorJson();<br /></span>
Copy after login

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 go to the document

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

When you see the return item, it is

{<span return</span>}    <span integer</span>    <span <strong>number of rows affected by the execution.</strong></span>
Copy after login

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/755383.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. I encountered such a problem when making the statement = Yii::app()- = - = 0 (! ( ['lid' = -update('emg_...
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