>  기사  >  데이터 베이스  >  Yii数据库操作_MySQL

Yii数据库操作_MySQL

WBOY
WBOY원래의
2016-06-01 13:07:221217검색

参考: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',]);


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.