国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP數據庫處理封裝類實例

2024-05-04 23:53:55
字體:
來源:轉載
供稿:網友

本文實例講述了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  //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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宜宾市| 高阳县| 景泰县| 泰州市| 文水县| 班玛县| 镇赉县| 柳江县| 慈利县| 涞源县| 佛坪县| 嘉黎县| 新巴尔虎右旗| 武功县| 桂林市| 武汉市| 葵青区| 开封县| 黑河市| 台安县| 永德县| 黔东| 朝阳市| 乌审旗| 崇礼县| 天气| 庆安县| 九台市| 兴山县| 洮南市| 桑日县| 罗田县| 论坛| 淮安市| 吉木乃县| 云安县| 云安县| 栾川县| 石家庄市| 洱源县| 银川市|