think\Loader::db PHP Method

db() public static method

数据库初始化 并取得数据库类实例
public static db ( mixed $config = [], boolean | string $name = false ) : Connection
$config mixed 数据库配置
$name boolean | string 连接标识 true 强制重新连接
return Think\Db\Connection
    public static function db($config = [], $name = false)
    {
        return Db::connect($config, $name);
    }

Usage Example

Example #1
0
 /**
  * 加载系统扩展配置
  */
 public static function load()
 {
     $config = \think\Cache::get('db_config_cache_data');
     if (!$config) {
         // 在这里先判断一下数据库是否已经正确安装
         $Db = \think\Loader::db();
         $Query = $Db->query("SHOW TABLES LIKE '" . \think\Config::get('database.prefix') . "config'");
         if (empty($Query)) {
             self::install();
         }
         $data = \think\Db::name('Config')->where('status', 1)->field('type,name,value')->select();
         $config = [];
         if ($data && is_array($data)) {
             foreach ($data as $value) {
                 $config[$value['name']] = self::parse($value['type'], $value['value']);
             }
         }
         \think\Cache::set('db_config_cache_data', $config);
     }
     \think\Config::set($config);
 }
All Usage Examples Of think\Loader::db