写一个数据库连接的单例模式案例

Original 2019-06-14 14:18:06 181
abstract:/* * 使用数据库 * 第一步:连接数入库 * 1、全局配置 \config\database.php 文件 * 2、动态配置(只适用一次使用) 方法在think\db\Query.php中有一个方法:connect() * public function conn2() * { *  return Db::connect([ *      'ty


/*
* 使用数据库
* 第一步:连接数入库
* 1、全局配置 \config\database.php 文件
* 2、动态配置(只适用一次使用) 方法在think\db\Query.php中有一个方法:connect()
* public function conn2()
* {
*  return Db::connect([
*      'type'=>'mysql',
*      'hostname'=>'localhost',
*      'database'=>'demo',
*      'username'=>'root',
*      'password'=>'root',
* ])
* -> table('student') -> where('id',2) -> value('name');
* }
*
*
* 3、DSN连接字符串
* 数据库类型://用户名:密码@数据库地址:端口号/数据库的名称#字符集
* public function conn3($dsn)
* {
*      $dsn = 'mysql://root:root@localhost:3306/demo#utf8';
*      return Db::table('student') -> where('id',5) -> value('name');
* }
*
*
* */

Correcting teacher:查无此人Correction time:2019-06-14 14:19:07
Teacher's summary:完成的不错。编程有很多设计模式,多了解,对以后工作帮助很大。继续加油。

Release Notes

Popular Entries