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 相关文章推荐
php 输出双引号&quot;与单引号'的方法
May 09 PHP
PHP下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
Jun 09 PHP
Apache下禁止php文件被直接访问的解决方案
Apr 25 PHP
thinkphp实现图片上传功能分享
Mar 04 PHP
CodeIgniter模板引擎使用实例
Jul 15 PHP
PHP图片自动裁切应付不同尺寸的显示
Oct 16 PHP
PHP生成不重复随机数的方法汇总
Nov 19 PHP
ThinkPHP入口文件设置及相关注意事项分析
Dec 05 PHP
php+ajax登录跳转登录实现思路
Jul 31 PHP
Linux平台php命令行程序处理管道数据的方法
Nov 10 PHP
基于laravel belongsTo使用详解
Oct 18 PHP
为你的 Laravel 验证器加上多验证场景的实现
Apr 07 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实现简单的语法高亮函数实例分析
2015/04/27 PHP
php结合正则批量抓取网页中邮箱地址
2015/05/19 PHP
PHP数据库处理封装类实例
2016/12/24 PHP
YII中Ueditor富文本编辑器文件和图片上传的配置图文教程
2017/03/15 PHP
JQuery 返回布尔值Is()条件判断方法代码
2012/05/14 Javascript
jquery实现的带缩略图的焦点图片切换(自动播放/响应鼠标动作)
2013/01/23 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
一步步教大家编写酷炫的导航栏js+css实现
2016/03/14 Javascript
jQuery表单验证插件解析(推荐)
2016/07/21 Javascript
js 实现省市区三级联动菜单效果
2017/02/20 Javascript
js实现简单的选项卡效果
2017/02/23 Javascript
VUE2.0+ElementUI2.0表格el-table循环动态列渲染的写法详解
2018/11/30 Javascript
js实现点赞效果
2020/03/16 Javascript
[47:55]Ti4第二日主赛事败者组 NaVi vs EG 1
2014/07/20 DOTA
详解python如何调用C/C++底层库与互相传值
2016/08/10 Python
python psutil模块使用方法解析
2019/08/01 Python
关于django 1.10 CSRF验证失败的解决方法
2019/08/31 Python
浅谈Python中re.match()和re.search()的使用及区别
2020/04/14 Python
pytorch中 gpu与gpu、gpu与cpu 在load时相互转化操作
2020/05/25 Python
Python xmltodict模块安装及代码实例
2020/10/05 Python
8款使用 CSS3 实现超炫的 Loading(加载)的动画效果
2015/03/17 HTML / CSS
CSS3的一个简单导航栏实现
2015/08/03 HTML / CSS
伦敦一家领先的精品零售商:IRIS Fashion
2019/05/24 全球购物
澳大利亚香水在线商店:City Perfume
2020/09/02 全球购物
会计专业自我鉴定范文
2013/10/06 职场文书
总经理秘书工作职责
2013/12/26 职场文书
卫校中专生的自我评价
2014/01/15 职场文书
商务英语广告词大全
2014/03/18 职场文书
应届毕业生求职信范文
2014/05/08 职场文书
社会实践活动总结范文
2014/07/03 职场文书
基层党建工作汇报材料
2014/08/15 职场文书
工作作风建设心得体会
2014/10/22 职场文书
2015年春节标语口号
2014/12/09 职场文书
婚礼父母致辞
2015/07/28 职场文书
tensorboard 可视化之localhost:6006不显示的解决方案
2021/05/22 Python
Ubuntu安装Mysql+启用远程连接的完整过程
2022/06/21 Servers