巅云智能建站平台搭建版(创业门户版)火爆上线,终身授权!新增:文章智能采集+全站真静态打包+城市分站+智能小程序+非法词过滤+H5自适应+智能链词等功能功能详情
建站资讯News

PHP数据库处理封装类实例

0
一佰互联网站建设(www.yinxi.net) 发布时间:2019-04-26 09:02:39 浏览数: 121

本文实例讲述了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+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php+Oracle数据库程序设计技巧总结》、《php+mssql数据库程序设计技巧总结》、《php+redis数据库程序设计技巧总结》、《php+mysqli数据库程序设计技巧总结》及《php常见数据库操作技巧汇总》

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

一佰互联是全国知名建站品牌服务商,我们有九年网站建设网站制作、网页设计、php开发和域名注册及虚拟主机服务经验,提供的自助建站服务更是全国有名。近年来还整合团队优势自主开发了可视化多用户”巅云建站系统“3.0平台版,拖拽排版网站制作设计,轻松实现pc站、手机微网站、小程序、APP一体化全网营销网站建设 ,已成功的为全国上百家网络公司提供自助建站平台搭建服务。更多资讯:tags标签

上一篇:PHP面向对象程序设计方法实例详解
下一篇: 如何判断php mysqli扩展类是否开启
[返回新闻列表]

相关新闻more

15
04月
丰都网站建设/网站设计/做网站/建网站哪家公

丰都网站建设/网站设计/做网站/建网站哪家公司服务好?推荐重庆一佰互联网络丰都网站建设公司服务好技术好客户满意度高首选 推荐 重庆一佰互联网... >>详情

22
11月
万州网站建设,网站推广,万州虚拟主机,网站优

万州网站建设|万州网站推广|万州虚拟主机|网站优化排名|万州建网站|万州网站开发|万州做网站公司|万州网站制作|万州网页设计公司|万州域名注... >>详情

25
05月
巅云话题丨先活在当下,再放眼未来

今天的话题是有点大了,至于是否合情,是否合理,每个人对这个世界都有不同的看法,我只是其中一个罢了。 ... >>详情

15
04月
高端网站建设-SEO优化中如何做好网站外部链

自从搜索引擎将网站的链接广泛度作为排名参考的重要因素以后,越多网站链接你,你的网站排名就越高。同时,链接的质量也是搜索引擎考虑的重要因素。如... >>详情

高端网站建设

美工兼顾SEO,为企业电子商务营销助力!

电话:

023-85725751
建站

产品

域名注册 虚拟主机 云服务器 企业邮局
智能建站 APP打包 微站/小程序 创业平台
网站推广 媒体营销 智能采集 AI机器人
400电话 短信营销 店销机器人
私人定制 门户网站