search
Homephp教程php手册Yii Query Builder insert()、update()、delete()使用

Yii自带的query builder还是很好用的,省去了拼sql的过程,今天在写一个语句的时候遇到这样一个问题

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

用 $operate_rst 来记录操作结果,执行新建insert没有问题,但是在更新时候,有时会显示操作失败,检查了半天,也找不到原因,只好去翻文档

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

看到return那一项是

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

瞬间明白问题了,因为有的时候可能没有改数据但是触发了更新操作,所以这时候受更改的行数为0,返回的判断就进入到错误代码里。。

同理,delete() 和 insert() 的方法返回值意义也是受到影响的行数,所以delete和insert可以根据返回值是否大于0来判断操作是否成功,但是update操作不一定,返回值为0也有可能表示对DB操作成功。

 

 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.