yii mysql 操作数据库_Yii数据库操作_MySQL

参考:https://github.com/yii2-chinesization/yii2-zh-cn/blob/master/guide-zh-CN/db-dao.md

返回多行:

$command = $connection->createCommand('SELECT * FROM post');$posts = $command->queryAll();

翻译单行:

$command = $connection->createCommand('SELECT * FROM post WHERE id=1');$post = $command->queryOne();

查询多列:

$command = $connection->createCommand('SELECT title FROM post');$titles = $command->queryColumn();

查询标量/计算值:

$command = $connection->createCommand('SELECT COUNT(*) FROM post');$postCount = $command->queryScalar();

更新数据:

$command = $connection->createCommand('UPDATE post SET status=1 WHERE id=1');$command->execute();

一次插入单行/多行:

// INSERT$connection->createCommand()->insert('user', [ 'name' => 'Sam', 'age' => 30,])->execute();// INSERT 一次插入多行$connection->createCommand()->batchInsert('user', ['name', 'age'], [ ['Tom', 30], ['Jane', 20], ['Linda', 25],])->execute();

更新:

$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();

删除:

$connection->createCommand()->delete('user', 'status = 0')->execute();

预处理:

$command = $connection->createCommand('SELECT * FROM post WHERE id=:id');$command->bindValue(':id', $_GET['id']);$post = $command->query();

一次预处理语句执行多次:

$command = $connection->createCommand('DELETE FROM post WHERE id=:id');$command->bindParam(':id', $id);$id = 1;$command->execute();$id = 2;$command->execute();

建表:

$connection->createCommand()->createTable('post', [ 'id' => 'pk', 'title' => 'string', 'text' => 'text',]);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值