yii增删改查操作数据库范例语句

  • 时间:
  • 浏览:19

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


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


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


4. 删除数据(delete)


$con = Yii::app()->db;

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