加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

yii框架builder、update、delete使用方法

发布时间:2020-12-12 20:09:59 所属栏目:PHP教程 来源:网络整理
导读:Yii自带的query builder还是很好用的,省去了拼sql的过程,今天在写一个语句的时候遇到这样一个问题 代码如下: $connection = Yii::app()->db; $command = $connection->createCommand(); $operate_rst = 0; if(!empty($_POST['lid'])){ $operate_rst = $com

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

代码如下:
$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();

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

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

看到return那一项是

代码如下:
{return} integer number of rows affected by the execution.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读