PHP连接mysql数据库之根据配置文件选择mysqli还是pdo方式
2017-09-24 20:36
926 查看
配置文件config.php
<?php return array( 'DB' => array( 'default_extension' => 'mysqli', ), );
接口I_DB.interface.php
<?php interface I_DB{ public static function getInstance($config); public function my_query($sql); public function fetchAll($sql); public function fetchRow($sql); public function fetchColumn($sql); }
Mysqli连接配置文件MysqliDB.class.php
<?php header("content-type:text/html;charset=utf-8"); include "./I_DB.interface.php"; class MysqliDB implements I_DB{ //私有的属性 private static $instance; //用来保存单例对象 private $host; //主机名称 private $port; //主机端口号 private $user; //数据库用户名 private $pass; //数据库密码 private $dbname; //要操作的数据库名称 private $charset; //要设置的字符集 private $link; //数据库连接句柄 /** * 私有的构造方法 * 初始化参数 * 数据库连接三步曲 * 1、连接数据库 * 2、选择数据库 * 3、设置字符集 */ private function __construct($config){ //初始化属性的值 $this->init($config); //连接数据库 $this->db_connect(); //选择数据库 $this->db_usedb(); //设置字符集 $this->db_charset(); } //接收配置参数 private function init($config){ $this->host = isset($config['host']) ? $config['host']:'localhost'; $this->port = isset($config['port']) ? $config['port'] : '3306'; $this->user = isset($config['user']) ? $config['user'] : 'root'; $this->pass = isset($config['pass']) ? $config['pass'] : ''; $this->dbname = isset($config['dbname']) ? $config['dbname'] : ''; $this->charset=isset($config['charset']) ? $config['charset'] : 'utf8'; } //连接数据库 private function db_connect(){ $this->link = mysqli_connect($this->host.':'.$this->port,$this->user,$this->pass); if(!$this->link){ echo "数据库连接失败! "; echo "错误编码:".mysqli_errno($this->link)." "; echo "错误信息:".mysqli_error($this->link)." "; exit; } } /** * 数据查询并对错误进行处理 * @param string $sql * @return mixed(book|resource) */ public function my_query($sql){ $res = mysqli_query($this->link,$sql); if(!$res){ echo "sql语句执行失败 "; echo "错误编码是".mysqli_errno($this->link)." "; echo "错误信息是".mysqli_error($this->link)." "; } return $res; } /** * 返回多选多列的结果集,二维数组 * @param string $sql 一条sql语句 * @return mixed(array|false) 执行成功是数组,失败是false */ public function fetchAll($sql){ //执行sql if($res = $this->my_query($sql)){ $rows = []; while($row = mysqli_fetch_assoc($res)){ $rows[] = $row; }; //结果集使用完毕,主动释放 mysqli_free_result($res); return $rows; }else{ return false; } } //返回一条记录 public function fetchRow($sql){ //先执行 if($res = $this->my_query($sql)){ $row = mysqli_fetch_assoc($res); mysqli_free_result($res); return $row; }else{ return false; } } //返回单一值,也就是单行单列 public function fetchColumn($sql){ //先执行 if($res = $this->my_query($sql)){ $row = mysqli_fetch_row($res); mysqli_free_result($res); return isset($row[0])?$row[0]:false ; }else{ return false; } } //设置字符集 private function db_charset(){ $sql = "set names $this->charset"; return $this->my_query($sql); } //选择数据库 private function db_usedb(){ $sql = "use $this->dbname"; return $this->my_query($sql); } //私有的克隆,不允许克隆创建对象 private function __clone(){ die('clone is not allowed'); } //公开的静态方法 public static function getInstance($config){ if(!self::$instance instanceof self){ self::$instance = new self($config); } return self::$instance; } public function __destruct(){ @mysqli_close($this->link); } }
PDO连接配置文件PDODB.class.php
<?php header('content-type:text/html;charset=utf-8'); include './I_DB.interface.php'; class PDODB implements I_DB{ //私有的属性 private static $instance; //用来保存单例对象 private $host; //主机名称 private $port; //主机端口号 private $user; //数据库用户名 private $pass; //数据库密码 private $dbname; //要操作的数据库名称 private $charset; //要设置的字符集 private $dsn; //数据资源名称 private $pdo; // pdo连接 /** * 私有的构造方法 * 初始化参数 * 数据库连接三步曲 * 1、连接数据库 * 2、选择数据库 * 3、设置字符集 */ private function __construct($config){ //初始化属性 $this->initParams($config); //初始化dsn $this->initDSN(); //初始化pdo $this->initPDO(); //初始化PDO对象的属性 $this->initAttribute(); } // 对外公开的入口 public static function getInstance($config){ if(!self::$instance instanceof self){ self::$instance = new self($config); } return self::$instance; } // 初始化参数 private function initParams($config){ $this->host = isset($config['host']) ? $config['host']:'localhost'; $this->port = isset($config['port']) ? $config['port'] : '3306'; $this->user = isset($config['user']) ? $config['user'] : 'root'; $this->pass = isset($config['pass']) ? $config['pass'] : ''; $this->dbname = isset($config['dbname']) ? $config['dbname'] : ''; $this->charset=isset($config['charset']) ? $config['charset'] : 'utf8'; } //初始化dsn private function initDSN(){ $this->dsn = "mysql:host=$this->host;port=$this->port;dbname=$this->dbname;charset=$this->charset"; } /** * 输出异常信息 */ private function my_error($e) { echo 'SQL语句执行失败!<br />'; echo '错误的代码是:', $e->getCode(), '<br />'; echo '错误的信息是:', $e->getMessage(), '<br />'; echo '错误的脚本是:', $e->getFile(), '<br />'; echo '错误的行号是:', $e->getLine(), '<br />'; return false; } //初始化pdo private function initPDO(){ try{ $this->pdo = new PDO($this->dsn,$this->user,$this->pass); }catch(Exception $e){ $this->my_error($e); } } // 初始化PDO对象的属性 private function initAttribute(){ $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } // 查询语句 public function my_query($sql){ try{ $result = $this->pdo->exec($sql); }catch(Exception $e){ $this->my_error($e); } return $result; } // 实现fetchAll抽象方法 public function fetchAll($sql){ try{ $stmt = $this->pdo->query($sql); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); // 关闭游标,释放结果集 $stmt->closeCursor(); }catch(Exception $e){ $this->my_error($e); } return $result; } public function fetchRow($sql){ try{ $stmt = $this->$pdo->query($sql); $result = $stmt->fetch(PDO::FETCH_ASSOC); // 关闭游标,释放结果集 $stmt->closeCursor(); }catch(Exception $e){ $this->my_error($e); } return $result; } public function fetchColumn($sql){ try{ $stmt = $this->pdo->query($sql); $result = $stmt->fetchColumn(); $stmt->closeCursor(); }catch(Exception $e){ $this->my_error($e); } return $result; } // 禁止克隆创建一个对象 private function __clone(){ die('clone is not allowed'); } }
使用方法database.php
<?php header("content-type:text/html;charset=utf-8"); $conf = include "./config.php"; $config = array( "pass" => "789890", "dbname" => "bbs" ); switch ($conf['DB']['default_extension']) { case 'mysqli': include "./MysqliDB.class.php"; $db = MysqliDB::getInstance($config); break; case 'pdo' : include "./PDODB.class.php"; $db = PDODB::getInstance($config); } $sql = "select * from user"; echo "<pre>"; var_dump($db->fetchAll($sql));
相关文章推荐
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- ThinkPHP3.0使用pdo方式连接sqlserver 2000、mssql 2000的配置,直接上配置文件,php版本是php5.2,服务器环境是ComsenzEXP
- php连接mysql数据库的几种方式(mysql、mysqli、pdo)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- php连接mysql数据库的几种方式(mysql、mysqli、pdo)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- php连接到MySQL数据库服务器时三种主要的API:mysql,mysqli,pdo区别及联系
- 搭建Windows系统下php开发环境有很多种方式,比如LAMP的配置、php IDE和调试工具的选择等等。根据每个人喜好不同有很多配置方式。这里主要介绍的是利用XAMPP集成安装和配置apache、
- Mac配置php.ini文件,连接mysql数据库
- php中内置的mysql数据库连接驱动mysqlnd简介及mysqlnd的配置安装方式
- php连接到MySQL数据库服务器时三种主要的API:mysql,mysqli,pdo区别及联系
- php pdo方式连接mysql数据库
- php中使用mysqli和pdo扩展,测试连接mysql数据库的效率。
- php中数据库连接方式pdo和mysqli对比分析
- php_mysql、php_mysqli 与 pdo_mysql 的区别与选择
- PHP PDO方式访问Mysql数据库实例
- ThinkPHP使用PDO方式连接数据库之PDO的介绍与安装,开发环境是PHP5