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 相关文章推荐
Zend Guard一些常见问题解答
Sep 11 PHP
linux下 C语言对 php 扩展
Dec 14 PHP
PHP表单提交表单名称含有点号(.)则会被转化为下划线(_)
Dec 14 PHP
php 删除一个数组中的某个值.兼容多维数组!
Feb 18 PHP
PHP实现数组递归转义的方法
Aug 28 PHP
CI框架Session.php源码分析
Nov 03 PHP
PHP实现上传文件并存进数据库的方法
Jul 16 PHP
PHP防止刷新重复提交页面的示例代码
Nov 11 PHP
WordPress中制作导航菜单的PHP核心方法讲解
Dec 11 PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 PHP
php微信开发之音乐回复功能
Jun 14 PHP
tp5递归 无限级分类详解
Oct 18 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警告Creating default object from empty value 问题的解决方法
2014/04/02 PHP
php比较两个字符串长度的方法
2015/07/13 PHP
扩展String功能方法
2006/09/22 Javascript
基于jquery的代码显示区域自动拉长效果
2011/12/07 Javascript
Google的跟踪代码 动态加载js代码方法应用
2012/11/12 Javascript
全面理解面向对象的 JavaScript(来自ibm)
2013/11/10 Javascript
JavaScript的原型继承详解
2015/02/15 Javascript
create-react-app使用antd按需加载的样式无效问题的解决
2019/02/26 Javascript
vue-cli3+ts+webpack实现多入口多出口功能
2019/05/30 Javascript
wepy--用vantUI 实现上弹列表并选择相应的值操作
2020/11/03 Javascript
[51:11]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS DT
2014/05/22 DOTA
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
Python设计模式之中介模式简单示例
2018/01/09 Python
Python3的介绍、安装和命令行的认识(推荐)
2018/10/20 Python
python 解压pkl文件的方法
2018/10/25 Python
python选取特定列 pandas iloc,loc,icol的使用详解(列切片及行切片)
2019/08/06 Python
TensorFlow基本的常量、变量和运算操作详解
2020/02/03 Python
解决python Jupyter不能导入外部包问题
2020/04/15 Python
Python闭包及装饰器运行原理解析
2020/06/17 Python
详解python定时简单爬取网页新闻存入数据库并发送邮件
2020/11/27 Python
详解python中的异常和文件读写
2021/01/03 Python
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
班级聚会策划书
2014/01/16 职场文书
淘宝店铺营销方案
2014/02/13 职场文书
夜不归宿检讨书
2014/02/25 职场文书
开业主持词
2014/03/21 职场文书
小学优秀辅导员事迹材料
2014/05/11 职场文书
教室布置标语
2014/06/26 职场文书
酒店采购员岗位职责
2015/04/03 职场文书
2015年电话销售工作总结范文
2015/04/20 职场文书
学习焦裕禄观后感
2015/06/09 职场文书
nginx 防盗链防爬虫配置详解
2021/03/31 Servers
Django中session进行权限管理的使用
2021/07/09 Python
MySQL非空约束(not null)案例讲解
2021/08/23 MySQL
Python 如何利用ffmpeg 处理视频素材
2021/11/27 Python
DE1103使用报告
2022/04/05 无线电