コード例 #1
0
ファイル: PdoMysql.php プロジェクト: nicklos17/eapi
 /**
  * 创建数据库实例
  *
  * @param object 数据库配置属性,包含 host|port|user|password|dbname|charset 6个属性
  * @param string DB编号key
  */
 private function __construct($config, $key)
 {
     try {
         // 数据库连接信息
         $dsn = "mysql:host={$config->host};port={$config->port};dbname={$config->dbname};charset=utf8";
         // 驱动选项
         $option = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_TO_STRING, \PDO::ATTR_TIMEOUT => 30);
         // 创建数据库驱动对象
         self::$instance[$key] = new \Pdo($dsn, $config->user, $config->password, $option);
     } catch (\Exception $e) {
         \core\Handler::appException($e);
         throw new \Exception('system error', '49901');
     }
 }
コード例 #2
0
ファイル: ModelBase.php プロジェクト: nicklos17/eapi
 /**
  * 执行sql查询
  *
  * @param string sql语句
  * @param array 参数数组
  * @param string 返回结果绑定到的对象
  * @param boolean 是否输出调试语句
  * @return void
  */
 public function query($sql, $params = array(), $class = 'stdClass', $debug = FALSE)
 {
     // 预处理绑定语句
     try {
         $this->stmt = $this->db->prepare($sql);
         if (!$this->stmt) {
             \core\Handler::appException('pdo prepare error with:' . $sql);
             throw new \Exception("system error", '49903');
         }
         // 参数绑定
         !$params or $this->bindValue($params);
         // 输出调试
         !$debug or $this->debug($sql, $params);
         // 执行一条sql语句
         if ($this->stmt->execute()) {
             // 设置解析模式
             $this->stmt->setFetchMode(\PDO::FETCH_CLASS, $class);
         } else {
             throw new \Exception('system error', '49904');
             // 获取数据库错误信息
             \core\Handler::appException($this->stmt->errorInfo()[2]);
         }
     } catch (\Exception $e) {
         \core\Handler::appException($e->getMessage());
         throw new \Exception('system error', '49902');
     }
 }