欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP数据库处理封装类实例

程序员文章站 2024-03-06 08:14:07
本文实例讲述了php数据库处理封装类。分享给大家供大家参考,具体如下: mysql的操作相关类,检查并使用了mysqli

本文实例讲述了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程序设计有所帮助。