一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

yii数据增加 删除 修改 查找的例子

时间:2022-06-25 00:53:07 编辑:袖梨 来源:一聚教程网

yii操作数据库的实现方式有很多种,以下总结出一套yii操作数据库的语句:

1. 查询数据(select

 代码如下 复制代码

$con = Yii::app()->db;
//查询一行数据
$noteInfo = $con->createCommand("select * from note where note_id='{$nid}'")->queryRow();
//查询多行数据
$noteInfo = $con->createCommand("select * from note where note_id='{$nid}'")->queryAll();

查询数据集合

 代码如下 复制代码

1、$admin=Admin::model()->findAll ($condition,$params);
该方法是根据一个条件查询一个集合,如: 
findAll("username=:name",array(":name"=>$username));   

2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,"name like ':name' and age=:age",array(':name'=>$name,'age'=>$age));   
该方法是根据主键查询一个集合,可以使用多个主键,如: 
findAllByPk(array(1,2));  

3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);

该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,如: 
findAllByAttributes(array('username'=>'admin'));

4、$admin=Admin::model()->findAllBySql($sql,$params);
该方法是根据SQL语句查询一个数组,如:
findAllBySql("select *from admin whereusername=:name",array(':name'=>'admin'));


2. 添加数据(insert)

$con = Yii::app()->db;
$datas['title'] = Yii::app()->request->getParam('title');
$datas['content'] = Yii::app()->request->getParam('content');;
$datas['add_time'] = time();
//入库
$res = $con->createCommand()->insert('note',$datas);

$admin=newAdmin;       
$admin->username=$username;
$admin->password=$password;
if($admin->save ()>0){
   echo "添加成功"; 
}else{ 
   echo "添加失败"; 
}
 
 
 
$userLimit = new UserLimit();
$userLimit->item = 0.30000;
if($userLimit->insert (){
 
}

3. 修改数据(update)

 代码如下 复制代码

$con = Yii::app()->db;
$content = 'abc';
$nid = '123';
$con->createCommand()->update('note',
 array(
 'content' => $content,
 'last_update' => time()
 ), 'note_id=:nid', array(':nid' => $nid));

$userLimitRet = UserLimit::model()->findByPk (array (
                                'user_id' => $userId,
                                'category_id' => $v
                        ));
$userLimitRet->order = $order;
if (! $userLimitRet->update ()) {
}
 
 
 
 
 
$userAmount=userAmount::model()->findByPk ($userId);
$userAmount->credit=Yii::app()->request->getParam('credit',10000);
$ret = $userAmount->save ();
 
 
 

1、Post::model()->updateAll ($attributes,$condition,$params);
 $count =Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
if($count>0){ 
   echo "修改成功"; 
}else{ 
   echo "修改失败"; 
}
 
 
 
 
 
$ret = $this->updateAll (array (
                                'set' => $Set
                        ), " user_id in ( " . $userIds . " ) and category_id = " . $categoryId);
if($ret){
    return true;
}else {
    return false;
}
 
  2、Post::model()->updateByPk ($pk,$attributes,$condition,$params);
 $count =Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin'));
$count =Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin'));
if($count>0){ 
   echo "修改成功"; 
}else{ 
   echo "修改失败"; 

4. 删除数据(delete)

 代码如下 复制代码

$con = Yii::app()->db;
$con->createCommand()->delete('note', 'note_id=:note_id',array(':note_id'=>123));

热门栏目