最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP數(shù)據(jù)庫處理封裝類實例

 更新時間:2016年12月24日 09:57:48   作者:trace332  
這篇文章主要介紹了PHP數(shù)據(jù)庫處理封裝類,結合完整實例形式分析了php基于mysqli封裝的數(shù)據(jù)庫連接及增刪改查等操作,需要的朋友可以參考下

本文實例講述了PHP數(shù)據(jù)庫處理封裝類。分享給大家供大家參考,具體如下:

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相關內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php+Oracle數(shù)據(jù)庫程序設計技巧總結》、《php+mssql數(shù)據(jù)庫程序設計技巧總結》、《php+redis數(shù)據(jù)庫程序設計技巧總結》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設計有所幫助。

相關文章

  • php在服務器執(zhí)行exec命令失敗的解決方法

    php在服務器執(zhí)行exec命令失敗的解決方法

    出于安全的原因,服務器是不允許php或者其他語言執(zhí)行exec命令的,當你有特殊需要php在服務器執(zhí)行exec命令時,你需要設置兩個地方,不然就無法執(zhí)行成功
    2012-03-03
  • php中try catch捕獲異常實例詳解

    php中try catch捕獲異常實例詳解

    這篇文章主要介紹了php中try catch捕獲異常的用法,以實例形式詳細分析了捕獲一個異常與捕獲多個異常的方法,有助于更好的處理一些不必要的錯誤,具有一定的實用價值,需要的朋友可以參考下
    2014-11-11
  • php中使用Akismet防止垃圾評論的代碼

    php中使用Akismet防止垃圾評論的代碼

    Akismet是一個優(yōu)秀的防Spam垃圾留言的優(yōu)秀插件,絕大多數(shù)wordpress blogger都在使用,有了akismet之后,基本上不用擔心垃圾留言的煩惱了。
    2011-06-06
  • PHP使用PHPExcel刪除Excel單元格指定列的方法

    PHP使用PHPExcel刪除Excel單元格指定列的方法

    這篇文章主要介紹了PHP使用PHPExcel刪除Excel單元格指定列的方法,涉及PHPExcel針對Excel單元格的遍歷操作及removeColumn方法刪除單元格的相關使用技巧,需要的朋友可以參考下
    2016-07-07
  • 微信公眾平臺接口開發(fā)入門示例

    微信公眾平臺接口開發(fā)入門示例

    這篇文章主要介紹了微信公眾平臺接口開發(fā)入門示例,較為簡單透徹的分析了微信公眾平臺接口開發(fā)的技巧與具體方法,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • php一些公用函數(shù)的集合

    php一些公用函數(shù)的集合

    php常用公用函數(shù)
    2008-03-03
  • 談談PHP連接Access數(shù)據(jù)庫的注意事項

    談談PHP連接Access數(shù)據(jù)庫的注意事項

    有的時候需要用php連接access數(shù)據(jù)庫,結果整了半天Access數(shù)據(jù)庫就是連接不上,查找很多資料,以下是些個人經(jīng)驗,希望能給需要連接access 數(shù)據(jù)的人帶來幫助。
    2016-08-08
  • php實現(xiàn)監(jiān)控varnish緩存服務器的狀態(tài)

    php實現(xiàn)監(jiān)控varnish緩存服務器的狀態(tài)

    這篇文章主要介紹了php實現(xiàn)監(jiān)控varnish緩存服務器的狀態(tài),Varnish是一款高性能的開源HTTP加速器,可以替代Squid、Nginx等服務器,需要的朋友可以參考下
    2014-12-12
  • php將金額數(shù)字轉化為中文大寫

    php將金額數(shù)字轉化為中文大寫

    本文給大家匯總介紹了幾種php將金額數(shù)字轉化為中文大寫的實用函數(shù),各有優(yōu)劣,小伙伴們根據(jù)自己的項目需求自由選擇吧。
    2015-07-07

最新評論

郧西县| 周至县| 鲁山县| 通渭县| 东乡族自治县| 台中县| 额尔古纳市| 昌邑市| 嵊州市| 万山特区| 肃宁县| 莎车县| 炉霍县| 广州市| 望江县| 贵德县| 托克逊县| 弥渡县| 永靖县| 南丹县| 荥阳市| 河津市| 红河县| 监利县| 清水河县| 顺义区| 华蓥市| 依兰县| 尼勒克县| 孟津县| 彭阳县| 万山特区| 天长市| 莎车县| 兰溪市| 吴忠市| 樟树市| 富源县| 会昌县| 神木县| 郯城县|