db() public static method

数据库初始化 并取得数据库类实例
public static db ( mixed $config = [], boolean | string $name = false ) : Connection
$config mixed 数据库配置
$name boolean | string 连接标识 true 强制重新连接
return Think\Db\Connection
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);
 }
Example #2
0
 public function testTable()
 {
     Loader::table('', ['connection' => ['type' => 'mysql', 'database' => 'test', 'username' => 'root', 'password' => '']]);
     Loader::db('mysql://root@127.0.0.1/test#utf8');
 }
Example #3
0
 /**
  * 修复表
  */
 public function repair($tables = '')
 {
     if ($tables) {
         $Db = Loader::db();
         if (is_array($tables)) {
             $tables = implode('`,`', $tables);
             $list = $Db->query("REPAIR TABLE `{$tables}`");
             if ($list) {
                 return $this->success("数据表修复完成!");
             } else {
                 return $this->error("数据表修复出错请重试!");
             }
         } else {
             $list = $Db->query("REPAIR TABLE `{$tables}`");
             if ($list) {
                 return $this->success("数据表'{$tables}'修复完成!");
             } else {
                 return $this->error("数据表'{$tables}'修复出错请重试!");
             }
         }
     } else {
         return $this->error("请指定要修复的表!");
     }
 }
Example #4
0
 public function import($start = 0)
 {
     //还原数据
     $db = \think\Loader::db();
     if ($this->config['compress']) {
         $gz = gzopen($this->file[1], 'r');
         $size = 0;
     } else {
         $size = filesize($this->file[1]);
         $gz = fopen($this->file[1], 'r');
     }
     $sql = '';
     if ($start) {
         $this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start);
     }
     for ($i = 0; $i < 1000; $i++) {
         $sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz);
         if (preg_match('/.*;$/', trim($sql))) {
             if (false !== $db->execute($sql)) {
                 $start += strlen($sql);
             } else {
                 return false;
             }
             $sql = '';
         } elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) {
             return 0;
         }
     }
     return [$start, $size];
 }
Example #5
0
 public function testTable()
 {
     Loader::db('mysql://root@127.0.0.1/test#utf8');
 }