首頁 > 資料庫 > mysql教程 > Yii数据库操作_MySQL

Yii数据库操作_MySQL

WBOY
發布: 2016-06-01 13:07:22
原創
1246 人瀏覽過

参考: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',]);
登入後複製


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板