标签:
继续《Yii Framework2.0开发教程(3)数据库mysql入门》
首先给予一定的尊重yii2数据库支持引进
Yii 基于 PHP‘s PDO一个成熟的数据库访问层的建立。它提供了一个统一的 API 并且克服了许多不同的 DBMS 违禁使用。 Yii 默认支持下面 DBMS :
MySQL
MariaDB
SQLite
PostgreSQL
CUBRID: version 9.1.0 or higher.
Oracle
MSSQL: version 2012 或更高版本号,如需使用 LIMIT/OFFSET。
配置
開始使用数据库首先须要配置数据库连接组件,通过加入 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),例如以下所看到的:
return [ // ... 'components' => [ // ... 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=mydatabase', // MySQL, MariaDB //'dsn' => 'sqlite:/path/to/database/file', // SQLite //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase', // MS SQL Server, sqlsrv driver //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver //'dsn' => 'mssql:host=localhost;dbname=mydatabase', // MS SQL Server, mssql driver //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle 'username' => 'root', //数据库用户名 'password' => '', //数据库密码 'charset' => 'utf8', ], ], // ... ];
在我们的样例中给出的配置文件是
<?php //配置的数据库连接能够在应用中通过 Yii::$app->db 訪问 return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=zhyoulun', 'username' => 'root', 'password' => '20092565', 'charset' => 'utf8', ];
第一步、数据库连接初始化的两种方式
(1)
$connection = Yii::$app->db; $connection->open();
(2)
$connection = new \yii\db\Connection( ['dsn' => 'mysql:host=localhost;dbname=zhyoulun', 'username' => 'root', 'password' => '20092565', 'charset' => 'utf8'] ); $connection->open();
$command = $connection->createCommand('SELECT * FROM country'); $countries = $command->queryAll(); echo '<pre>'; print_r($countries); echo '</pre>';
整个helloworld.php代码例如以下
<?php //$connection = Yii::$app->db; //$connection->open(); $connection = new \yii\db\Connection( ['dsn' => 'mysql:host=localhost;dbname=zhyoulun', 'username' => 'root', 'password' => '20092565', 'charset' => 'utf8'] ); $connection->open(); $command = $connection->createCommand('SELECT * FROM country'); $countries = $command->queryAll(); echo '<pre>'; print_r($countries); echo '</pre>'; ?>
(1)queryOne。返回单行
$command = $connection->createCommand("SELECT * FROM country WHERE code='BR'"); $country = $command->queryOne(); echo '<pre>'; print_r($country); echo '</pre>';
(2)queryColumn,查询多列值
$command = $connection->createCommand("SELECT code FROM country"); $country = $command->queryColumn(); echo '<pre>'; print_r($country); echo '</pre>';
(3)queryScalar,查询标量值/计算值
$command = $connection->createCommand("SELECT count(*) FROM country"); $country = $command->queryScalar(); echo '<pre>'; print_r($country); echo '</pre>';
(4)假设运行 SQL 不返回不论什么数据可使用命令中的 execute 方法(UPDATE, INSERT, DELETE 更新、插入和删除等)
更新
$command = $connection->createCommand("UPDATE country SET name='Brazil-haha' WHERE code='BR'"); $command->execute();
更新
$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
$connection->createCommand()->insert('user', [ 'name' => 'Sam', 'age' => 30, ])->execute();
$connection->createCommand()->batchInsert('user', ['name', 'age'], [ ['Tom', 30], ['Jane', 20], ['Linda', 25], ])->execute();
$connection->createCommand()->delete('user', 'status = 0')->execute();
%======================================切割线======================================%
$command = $query->createCommand(); $rows = $command->queryAll();
$query = new \yii\db\Query; //组织查询语句 $query->select('code')->from('country')->limit(5); //编译并运行查询语句 $row = $query->all(); echo '<pre>'; print_r($row); echo '</pre>';
其它函数如where、groupby、orderby、having等參见http://www.yiichina.com/guide/2/db-query-builder
參考:
https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-dao.md
http://www.yiichina.com/api/2.0/yii-db-query
http://www.yiichina.com/guide/2/db-query-builder
转载请注明出处:http://blog.csdn.net/zhyoulun/article/details/40476019
版权声明:本文博主原创文章。博客,未经同意不得转载。
Yii Framework2.0开发教程(5)数据库mysql性能
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4821416.html