YII框架数据库的增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了YII框架数据库的增删改查相关的知识,希望对你有一定的参考价值。
- //添加
- Yii::app()->db->createCommand()->insert(‘tbl_user‘,
- array(
- ‘username‘ => ‘zhangsan‘,
- ‘email‘ => ‘[email protected]‘));
- //使用user model添加数据
- $user = new Users();
- $user->setAttributes(array(
- ‘username‘ => ‘user1‘,
- ‘email‘ => ‘[email protected]‘,
- ‘password‘=>md5("abcsd5a4"),
- ‘last_login‘=>time()
- ));
- $user->save()
- //修改
- Yii::app()->db->createCommand()->update(‘tbl_user‘,
- array(
- ‘username‘ => ‘Tester‘), ‘id=:id‘, array(
- ‘:id‘ => 3));
- //删除
- Yii::app()->db->createCommand()->delete(‘tbl_user‘, ‘id=:id‘,
- array(
- ‘:id‘ => 2));
- //查询 - 多条数据
- $users = Yii::app()->db->createCommand()
- ->select(‘*‘)
- ->from(‘tbl_user‘)
- ->queryAll();
- //查询一条数据
- $sql = "SELECT id,name FROM tbl_user WHERE email=‘[email protected]‘ ";
- $users=Yii::app()->db->createCommand($sql)->queryRow();
- var_dump($users);
以上是关于YII框架数据库的增删改查的主要内容,如果未能解决你的问题,请参考以下文章