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 相关文章推荐
第七节 类的静态成员 [7]
Oct 09 PHP
开发大型PHP项目的方法
Oct 09 PHP
也谈php网站在线人数统计
Apr 09 PHP
PHP+MySQL 制作简单的留言本
Nov 02 PHP
php 操作调试的方法
Jul 12 PHP
用PHP实现弹出消息提示框的两种方法
Dec 17 PHP
php弹出对话框实现重定向代码
Jan 23 PHP
php制作中间带自己定义图片二维码的方法
Jan 27 PHP
Laravel框架数据库CURD操作、连贯操作总结
Sep 03 PHP
PHP制作登录异常ip检测功能的实例代码
Nov 16 PHP
PHP封装的PDO数据库操作类实例
Jun 21 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
Oct 23 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 服务器调试 Zend Debugger 的安装教程
2009/09/25 PHP
PHP基础学习小结
2011/04/17 PHP
php对大文件进行读取操作的实现代码
2013/01/23 PHP
js 覆盖和重载 函数
2009/09/25 Javascript
js中函数调用的两种常用方法使用介绍
2014/07/17 Javascript
JS+CSS实现感应鼠标渐变显示DIV层的方法
2015/02/20 Javascript
JavaScript实现简单图片翻转的方法
2015/04/17 Javascript
js实现漂浮回顶部按钮实例
2015/05/06 Javascript
18个非常棒的jQuery代码片段
2015/11/02 Javascript
jQuery封装的屏幕居中提示信息代码
2016/06/08 Javascript
一个极为简单的requirejs实现方法
2016/10/20 Javascript
jQury Ajax使用Token验证身份实例代码
2017/09/22 Javascript
JavaScript自执行函数和jQuery扩展方法详解
2017/10/27 jQuery
JavaScript+Canvas实现彩色图片转换成黑白图片的方法分析
2018/07/31 Javascript
Layer弹出层动态获取数据的方法
2018/08/20 Javascript
解决vue props 拿不到值的问题
2018/09/11 Javascript
深入了解JavaScript 防抖和节流
2019/09/12 Javascript
JavaScript实现简单验证码
2020/08/24 Javascript
通过实例解析JavaScript常用排序算法
2020/09/02 Javascript
Python实现约瑟夫环问题的方法
2016/05/03 Python
Python随机数random模块使用指南
2016/09/09 Python
深入理解python对json的操作总结
2017/01/05 Python
python基础之入门必看操作
2017/07/26 Python
python 禁止函数修改列表的实现方法
2017/08/03 Python
带你认识Django
2019/01/15 Python
Python3.5 Pandas模块缺失值处理和层次索引实例详解
2019/04/23 Python
Python无头爬虫下载文件的实现
2020/04/02 Python
python实现按键精灵找色点击功能教程,使用pywin32和Pillow库
2020/06/04 Python
英国在线电子和小工具商店:TecoBuy
2018/10/06 全球购物
硕士研究生求职自荐信范文
2014/03/11 职场文书
大学生社会实践自我鉴定
2014/03/24 职场文书
运动会铅球比赛加油稿
2014/09/26 职场文书
2014年班干部工作总结
2014/11/25 职场文书
2014年个人师德工作总结
2014/12/04 职场文书
表扬稿范文
2015/01/17 职场文书
nginx如何将http访问的网站改成https访问
2021/03/31 Servers