单例 封装PHP 数据库连接
来源:互联网 发布:淘宝拍卖的电脑靠谱吗? 编辑:程序博客网 时间:2024/06/06 14:14
<?phpclass MySQLDB {private $host; //主机IPprivate $port; //端口号private $user; //用户名private $pwd; //密码private $charset; //字符编码private $dbname; //连接的数据库private $link; //保存数据库连接对象【可以省略】private static $instance; //保存MySQLDB的实例//初始化连接数据库参数private function initParam($config) {$this->host=isset($config['host'])?$config['host']:'';$this->port=isset($config['port'])?$config['port']:'3306';$this->user=isset($config['user'])?$config['user']:'';$this->pwd=isset($config['pwd'])?$config['pwd']:'';$this->charset=isset($config['charset'])?$config['charset']:'utf8';$this->dbname=isset($config['dbname'])?$config['dbname']:'';}//连接数据库private function connect() {$this->link=@mysql_connect("{$this->host}:{$this->port}",$this->user,$this->pwd) or die('数据库连接失败');}//设置字符编码private function setCharset() {$sql="set names '{$this->charset}'";$this->query($sql);}//选择数据库private function selectDB() {$sql="use `{$this->dbname}`";$this->query($sql);}/***此方法用来执行SQL语句*如果是数据查询语句,成功返回结果集,失败返回false*如果是数据操作语句,成功返回true,失败返回false;*/public function query($sql) {if(!$result=mysql_query($sql,$this->link)){echo 'SQL语句执行失败<br>';echo '错误编号:'.mysql_errno(),'<br>';echo '错误信息:'.mysql_error(),'<br>';echo '错误的SQL语句',$sql,'<br>';exit;}return $result;}/***私有的构造函数防止在类的外部实例化对象*@param $config array 连接数据库的参数*/private function __construct($config) {$this->initParam($config);$this->connect();$this->setCharset();$this->selectDB();}//私有的__clone()用来阻止是类的外部克隆对象private function __clone() {}/***公有的静态方法用来获取MySQLDB类的实例*@return object MySQLDB的实例*/public static function getInstance($config=array()) {if(!self::$instance instanceof self)self::$instance=new self($config);return self::$instance;}/***从数据库获取所有数据*@param $sql string SQL语句*@param $fetch_type string assoc|row|array*@return array 将表中的数据匹配成二维数组返回*/public function fetchAll($sql,$fetch_type='assoc') {$rs=$this->query($sql);$fetch_types=array('assoc','row','array');if(!in_array($fetch_type,$fetch_types))$fetch_type='assoc';$fetch_fun='mysql_fetch_'.$fetch_type;$array=array();while($rows=$fetch_fun($rs)){$array[]=$rows;}return $array;}/***获取记录的第一条记录*/public function fetchRow($sql,$fetch_type='assoc') {$rs=$this->fetchAll($sql,$fetch_type);return empty($rs)?null:$rs[0];}/***获取记录的第一行第一列*/public function fetchColumn($sql) {$rs=$this->fetchRow($sql,'row');return empty($rs)?null:$rs[0];}}header('content-type:text/html;charset=utf-8');//测试$config=array('host' => '127.0.0.1','user' => 'root','pwd' => 'aa','dbname'=> 'php4');$db=MySQLDB::getInstance($config);//$rs=$db->fetchRow("select * from stu",'aa');$rs=$db->fetchColumn("select * from stu");echo '<pre>';var_dump($rs);
阅读全文
0 0
- 单例 封装PHP 数据库连接
- php应用数据库连接中的单例模式
- php单例模式使用实例之数据库连接
- PHP中的单例模式(可用于数据库连接维护)
- 单例模式--数据库连接
- php单例模式连接数据库封装类
- php基于单例模式封装mysql类完整实例
- php基于单例模式封装mysql类完整实例
- PHP封装的一个单例模式Mysql操作类
- php单例模式连接数据库封装类
- 单例模式创建数据库连接
- sqlite单例封装
- AFNetworking封装单例
- okHttp单例封装
- 单例模式的数据库连接和PHP实现页面静态化的第一种方法
- WEB应用之: 对PHP数据库连接的简单封装
- 单例的一种封装
- OC单例 模板封装
- 文字识别
- ES6-正则的扩展-u、y修饰符
- jitpack让使用第三方依赖库更简单
- null和undefined的区别?
- zh找不到或无法加载主类
- 单例 封装PHP 数据库连接
- cpu
- Nutch2.X使用入坑总结
- lnmp环境下ThinkPHP3.2出现404 Not Found的问题
- Web/APP项目结构组织
- VS2010+Source Insight +PC-Lint开发环境搭建
- org.apache.ibatis.binding.BindingException: Mapper method 'com... has an unsupported return type
- wpf DoubleAnimation对image控件进行缩放旋转
- 百度音乐api接口