继续《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