<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 創建core/Database.php ``` <?php namespace core; use core\database\connection\MysqlConnection; class Database { protected $connections = []; // all connection // 獲取默認鏈接 protected function getDefaultConnection() { return \App::getContainer()->get('config')->get('database.default'); } // 設置默認鏈接 public function setDefaultConnection($name) { \App::getContainer()->get('config')->set('database.default', $name); } // 根據配置信息的name來創建鏈接 public function connection($name = null) { if (isset($this->connections[$name])) // 如果存在就直接返回 return $this->connections[$name]; if ($name == null) // 選擇默認鏈接 $name = $this->getDefaultConnection(); $config = \App::getContainer()->get('config')->get('database.connections.' . $name); // 獲取鏈接的配置 $connectionClass = null; // 鏈接處理的類 switch ($config['driver']) { case 'mysql': $connectionClass = MysqlConnection::class; break; // 如果有其他類型的數據庫 那就繼續完善 } $dsn = sprintf('%s:host=%s;dbname=%s', $config['driver'], $config['host'], $config['dbname']); try { $pdo = new \PDO($dsn, $config['username'], $config['password'], $config['options']); } catch (\PDOException $e) { die($e->getMessage()); } return $this->connections[$name] = new $connectionClass($pdo, $config); } // 代理模式 public function __call($method, $parameters) { return $this->connection()->$method(...$parameters); } } ``` ## 創建core/database/connection/Connection.php ``` <?php namespace core\database\connection; // 鏈接的基礎類 class Connection { protected $pdo; protected $tablePrefix; protected $config; public function __construct($pdo, $config) { $this->pdo = $pdo; $this->tablePrefix = $config['prefix']; $this->config = $config; } } ``` ## 創建core/database/connection/MysqlConnection.php ``` <?php namespace core\database\connection; // 繼承基礎類 class MysqlConnection extends Connection { protected static $connection; public function getConnection() { return self::$connection; } // 執行sql public function select($sql, $bindings = [], $useReadPdo = true) { $statement = $this->pdo; $sth = $statement->prepare($sql); try { $sth->execute( $bindings); return $sth->fetchAll(); } catch (\PDOException $exception){ echo ($exception->getMessage()); } } } ``` ## 綁定db ![](https://img.kancloud.cn/3a/e9/3ae9e95fd3e26ed1c89419a80c5e15e8_727x322.png) ## 運行 ![](https://img.kancloud.cn/71/24/712426b4f6c86382a9bd02ee715bf8cb_727x240.png) ![](https://img.kancloud.cn/0c/93/0c933a58d25a79f8ee85c3ef1d3d3089_877x413.png) ### 關于數據庫配置 config/database.php ``` <?php return [ 'default' => 'mysql_one', 'connections' => [ 'mysql_one' => [ 'driver' => 'mysql', 'host' => '134.175.80.215', // 這是我的一個遠程服務器 沒用的 但是也別亂搞 'username' => 'php_frame', 'dbname' => 'php_frame', 'password' => '12345678', 'prefix' => '', 'options' => [ ] ], ] ]; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看