返回写一个数据库连......登陆

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

龚一2019-06-14 14:18:06185


/*
* 使用数据库
* 第一步:连接数入库
* 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');
* }
*
*
* */

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送