PHP数据库处理封装类实例


Posted in PHP onDecember 24, 2016

本文实例讲述了PHP数据库处理封装类。分享给大家供大家参考,具体如下:

MySQL的操作相关类,检查并使用了mysqli

<?php
  //sample15_12.php
  class mydb {
    private $user;
    private $pass;
    private $host;
    private $db;
    //Constructor function.
    public function __construct (){
      $num_args = func_num_args();
      if($num_args > 0){
        $args = func_get_args();
        $this->host = $args[0];
        $this->user = $args[1];
        $this->pass = $args[2];
        $this->connect();
      }
    }
    //Function to tell us if mysqli is installed.
    private function mysqliinstalled (){
      if (function_exists ("mysqli_connect")){
        return true;
      } else {
        return false;
      }
    }
    //Function to connect to the database.
    private function connect (){
      try {
        //Mysqli functionality.
        if ($this->mysqliinstalled()){
          if (!$this->db = new mysqli ($this->host,$this->user,$this->pass)){
            $exceptionstring = "Error connection to database: <br />";
            $exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();
            throw new exception ($exceptionstring);
          }
        //Mysql functionality.
        } else {
          if (!$this->db = mysql_connect ($this->host,$this->user,$this->pass)){
            $exceptionstring = "Error connection to database: <br />";
            $exceptionstring .= mysql_errno() . ": " . mysql_error();
            throw new exception ($exceptionstring);
          }
        }
      } catch (exception $e) {
        echo $e->getmessage();
      }
    }
    //Function to select a database.
    public function selectdb ($thedb){
      try {
        //Mysqli functionality.
        if ($this->mysqliinstalled()){
          if (!$this->db->select_db ($thedb)){
            $exceptionstring = "Error opening database: $thedb: <br />";
            $exceptionstring .= $this->db->errno . ": " . $this->db->error;
            throw new exception ($exceptionstring);
          }
        //Mysql functionality.
        } else {
          if (!mysql_select_db ($thedb, $this->db)){
            $exceptionstring = "Error opening database: $thedb: <br />";
            $exceptionstring .= mysql_errno() . ": " . mysql_error();
            throw new exception ($exceptionstring);
          }
        }
      } catch (exception $e) {
        echo $e->getmessage();
      }
    }
    //Function to perform a query.
    public function execute ($thequery){
      try {
        //Mysqli functionality.
        if ($this->mysqliinstalled()){
          if (!$this->db->query ($thequery)){
            $exceptionstring = "Error performing query: $thequery: <br />";
            $exceptionstring .= $this->db->errno . ": " . $this->db->error;
            throw new exception ($exceptionstring);
          } else {
            echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.<br />";
          }
        //Mysql functionality.
        } else {
          if (!mysql_query ($thequery, $this->db)){
            $exceptionstring = "Error performing query: $thequery: <br />";
            $exceptionstring .= mysql_errno() . ": " . mysql_error();
            throw new exception ($exceptionstring);
          } else {
            echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
          }
        }
      } catch (exception $e) {
        echo $e->getmessage();
      }
    }
    //Function to return a row set.
    public function getrows ($thequery){
      try {
        //Mysqli functionality.
        if ($this->mysqliinstalled()){
          if ($result = $this->db->query ($thequery)){
            $returnarr = array ();
            while ($adata = $result->fetch_array ()){
              $returnarr = array_merge ($returnarr,$adata);
            }
            return $returnarr;
          } else {
            $exceptionstring = "Error performing query: $thequery: <br />";
            $exceptionstring .= $this->db->errno . ": " . $this->db->error;
            throw new exception ($exceptionstring);
          }
        //Mysql functionality.
        } else {
          if (!$aquery = mysql_query ($thequery)){
            $exceptionstring = "Error performing query: $thequery: <br />";
            $exceptionstring .= mysql_errno() . ": " . mysql_error();
            throw new exception ($exceptionstring);
          } else {
            $returnarr = array ();
            while ($adata = mysql_fetch_array ($aquery)){
              $returnarr = array_merge ($returnarr,$adata);
            }
            return $returnarr;
          }
        }
      } catch (exception $e) {
        echo $e->getmessage();
      }
    }
    //Function to close the database link.
    public function __destruct() {
      try {
        //Mysqli functionality.
        if ($this->mysqliinstalled()){
          if (!$this->db->close()){
            $exceptionstring = "Error closing connection: <br />";
            $exceptionstring .= $this->db->errno . ": " . $this->db->error;
            throw new exception ($exceptionstring);
          }
        //Mysql functionality.
        } else {
          if (!mysql_close ($this->db)){
            $exceptionstring = "Error closing connection: <br />";
            $exceptionstring .= mysql_errno() . ": " . mysql_error();
            throw new exception ($exceptionstring);
          }
        }
      } catch (exception $e) {
        echo $e->getmessage();
      }
    }
  }
  //Now, let us create an instance of mydb.
  $mydb = new mydb ("localhost","root","");
  //Select a database to use.
  $mydb->selectdb ("wps");
  //Now, let's perform an action.
  //$adata = $mydb->execute ("UPDATE cd SET title='Hybrid Theory' WHERE cdid='2'");
  //Then, let's try to return a row set.
  $adata = $mydb->getrows ("SELECT * FROM wp_terms");
  for ($i = 0; $i < count ($adata); $i++){
    echo $adata[$i] . "<br />";
  }
  $mydb->selectdb("test");
  $result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2");
  echo "<br />";
  echo $result;
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
JAVA/JSP学习系列之四
Oct 09 PHP
一个程序下载的管理程序(三)
Oct 09 PHP
Windows中安装Apache2和PHP4权威指南
Nov 18 PHP
php读取30天之内的根据算法排序的代码
Apr 06 PHP
ThinkPHP上使用多说评论插件的方法
Oct 31 PHP
php实现使用正则将文本中的网址转换成链接标签
Dec 03 PHP
Yii实现多数据库主从读写分离的方法
Dec 29 PHP
PHP速成大法
Jan 30 PHP
PHP SPL标准库之数据结构堆(SplHeap)简单使用实例
May 12 PHP
关于PHP 如何用 curl 读取 HTTP chunked 数据
Feb 26 PHP
php自定义扩展名获取函数示例
Dec 12 PHP
PHP convert_cyr_string()函数讲解
Feb 13 PHP
如何判断php mysqli扩展类是否开启
Dec 24 #PHP
Thinkphp框架中D方法与M方法的区别
Dec 23 #PHP
ThinkPHP 整合Bootstrap Ajax分页样式
Dec 23 #PHP
php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
Dec 23 #PHP
php的4种常用运行方式详解
Dec 22 #PHP
php curl 模拟登录并获取数据实例详解
Dec 22 #PHP
使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
Dec 21 #PHP
You might like
php 计划任务 检测用户连接状态
2012/03/29 PHP
单台服务器的PHP进程之间实现共享内存的方法
2014/06/13 PHP
PHP生成短网址的3种方法代码实例
2014/07/08 PHP
php开发工具有哪五款
2015/11/09 PHP
PHP使用Redis替代文件存储Session的方法
2017/02/15 PHP
jquery tab插件制作实现代码
2010/06/22 Javascript
JavaScript 操作键盘的Enter事件(键盘任何事件),兼容多浏览器
2010/10/11 Javascript
精选的10款用于构建良好易用性网站的jQuery插件
2011/01/23 Javascript
js点击页面其它地方将某个显示的DIV隐藏
2012/07/12 Javascript
javascript修改表格背景色实例代码分享
2013/12/10 Javascript
jQuery判断当前点击的是第几个li的代码
2014/09/26 Javascript
JavaScript中的alert()函数使用技巧详解
2014/12/29 Javascript
基于Bootstrap里面的Button dropdown打造自定义select
2016/05/30 Javascript
JavaScript浮点数及运算精度调整详解
2016/10/21 Javascript
JavaScript利用Date实现简单的倒计时实例
2017/01/12 Javascript
javascript  数组排序与对象排序的实例
2017/07/17 Javascript
mpvue全局引入sass文件的方法步骤
2019/03/06 Javascript
vue中对象数组去重的实现
2020/02/06 Javascript
小程序使用分包的示例代码
2020/03/23 Javascript
[04:41]2014DOTA2国际邀请赛 Liquid顺利突围晋级正赛
2014/07/09 DOTA
python 动态获取当前运行的类名和函数名的方法
2014/04/15 Python
python按综合、销量排序抓取100页的淘宝商品列表信息
2018/02/24 Python
python读取txt文件中特定位置字符的方法
2018/12/24 Python
python绘制彩虹图
2019/12/16 Python
python爬虫scrapy基本使用超详细教程
2021/02/20 Python
采用冷却技术的超自然舒适度:GhostBed床垫
2018/09/18 全球购物
英国领先的酒杯和水晶玻璃器皿制造商:Dartington Crystal
2019/06/23 全球购物
英国运动服、设备及配件网站:DW Sports
2019/12/04 全球购物
介绍下static、final、abstract区别
2015/01/30 面试题
毕业自我鉴定怎么写
2014/03/25 职场文书
四年级学生评语大全
2014/04/21 职场文书
委托培训协议书
2014/11/17 职场文书
2014年工商所工作总结
2014/12/09 职场文书
公证书格式
2015/01/23 职场文书
一文搞懂Golang 时间和日期相关函数
2021/12/06 Golang
实现GO语言对数组切片去重
2022/04/20 Golang