巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
建站资讯News

PHP数据库处置封装类实例

0
一佰互联网站扶植(www.taishanly.com) 宣布时候:2020-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德律风 短信营销 店销机械人
私家定制 流派网站