服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - PHP数据库处理封装类实例

PHP数据库处理封装类实例

2021-04-09 16:00trace332 PHP教程

这篇文章主要介绍了PHP数据库处理封装类,结合完整实例形式分析了php基于mysqli封装的数据库连接及增删改查等操作,需要的朋友可以参考下

本文实例讲述了PHP数据库处理封装类。分享给大家供大家参考,具体如下:

MySQL的操作相关类,检查并使用了mysqli

  1. <?php 
  2.   //sample15_12.php 
  3.   class mydb { 
  4.     private $user; 
  5.     private $pass; 
  6.     private $host; 
  7.     private $db; 
  8.     //Constructor function. 
  9.     public function __construct (){ 
  10.       $num_args = func_num_args(); 
  11.       if($num_args > 0){ 
  12.         $args = func_get_args(); 
  13.         $this->host = $args[0]; 
  14.         $this->user = $args[1]; 
  15.         $this->pass = $args[2]; 
  16.         $this->connect(); 
  17.       } 
  18.     } 
  19.     //Function to tell us if mysqli is installed. 
  20.     private function mysqliinstalled (){ 
  21.       if (function_exists ("mysqli_connect")){ 
  22.         return true
  23.       } else { 
  24.         return false
  25.       } 
  26.     } 
  27.     //Function to connect to the database. 
  28.     private function connect (){ 
  29.       try { 
  30.         //Mysqli functionality. 
  31.         if ($this->mysqliinstalled()){ 
  32.           if (!$this->db = new mysqli ($this->host,$this->user,$this->pass)){ 
  33.             $exceptionstring = "Error connection to database: <br />"
  34.             $exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error(); 
  35.             throw new exception ($exceptionstring); 
  36.           } 
  37.         //Mysql functionality. 
  38.         } else { 
  39.           if (!$this->db = mysql_connect ($this->host,$this->user,$this->pass)){ 
  40.             $exceptionstring = "Error connection to database: <br />"
  41.             $exceptionstring .= mysql_errno() . ": " . mysql_error(); 
  42.             throw new exception ($exceptionstring); 
  43.           } 
  44.         } 
  45.       } catch (exception $e) { 
  46.         echo $e->getmessage(); 
  47.       } 
  48.     } 
  49.     //Function to select a database. 
  50.     public function selectdb ($thedb){ 
  51.       try { 
  52.         //Mysqli functionality. 
  53.         if ($this->mysqliinstalled()){ 
  54.           if (!$this->db->select_db ($thedb)){ 
  55.             $exceptionstring = "Error opening database: $thedb: <br />"
  56.             $exceptionstring .= $this->db->errno . ": " . $this->db->error; 
  57.             throw new exception ($exceptionstring); 
  58.           } 
  59.         //Mysql functionality. 
  60.         } else { 
  61.           if (!mysql_select_db ($thedb, $this->db)){ 
  62.             $exceptionstring = "Error opening database: $thedb: <br />"
  63.             $exceptionstring .= mysql_errno() . ": " . mysql_error(); 
  64.             throw new exception ($exceptionstring); 
  65.           } 
  66.         } 
  67.       } catch (exception $e) { 
  68.         echo $e->getmessage(); 
  69.       } 
  70.     } 
  71.     //Function to perform a query. 
  72.     public function execute ($thequery){ 
  73.       try { 
  74.         //Mysqli functionality. 
  75.         if ($this->mysqliinstalled()){ 
  76.           if (!$this->db->query ($thequery)){ 
  77.             $exceptionstring = "Error performing query: $thequery: <br />"
  78.             $exceptionstring .= $this->db->errno . ": " . $this->db->error; 
  79.             throw new exception ($exceptionstring); 
  80.           } else { 
  81.             echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.<br />"
  82.           } 
  83.         //Mysql functionality. 
  84.         } else { 
  85.           if (!mysql_query ($thequery, $this->db)){ 
  86.             $exceptionstring = "Error performing query: $thequery: <br />"
  87.             $exceptionstring .= mysql_errno() . ": " . mysql_error(); 
  88.             throw new exception ($exceptionstring); 
  89.           } else { 
  90.             echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />"
  91.           } 
  92.         } 
  93.       } catch (exception $e) { 
  94.         echo $e->getmessage(); 
  95.       } 
  96.     } 
  97.     //Function to return a row set. 
  98.     public function getrows ($thequery){ 
  99.       try { 
  100.         //Mysqli functionality. 
  101.         if ($this->mysqliinstalled()){ 
  102.           if ($result = $this->db->query ($thequery)){ 
  103.             $returnarr = array (); 
  104.             while ($adata = $result->fetch_array ()){ 
  105.               $returnarr = array_merge ($returnarr,$adata); 
  106.             } 
  107.             return $returnarr; 
  108.           } else { 
  109.             $exceptionstring = "Error performing query: $thequery: <br />"
  110.             $exceptionstring .= $this->db->errno . ": " . $this->db->error; 
  111.             throw new exception ($exceptionstring); 
  112.           } 
  113.         //Mysql functionality. 
  114.         } else { 
  115.           if (!$aquery = mysql_query ($thequery)){ 
  116.             $exceptionstring = "Error performing query: $thequery: <br />"
  117.             $exceptionstring .= mysql_errno() . ": " . mysql_error(); 
  118.             throw new exception ($exceptionstring); 
  119.           } else { 
  120.             $returnarr = array (); 
  121.             while ($adata = mysql_fetch_array ($aquery)){ 
  122.               $returnarr = array_merge ($returnarr,$adata); 
  123.             } 
  124.             return $returnarr; 
  125.           } 
  126.         } 
  127.       } catch (exception $e) { 
  128.         echo $e->getmessage(); 
  129.       } 
  130.     } 
  131.     //Function to close the database link. 
  132.     public function __destruct() { 
  133.       try { 
  134.         //Mysqli functionality. 
  135.         if ($this->mysqliinstalled()){ 
  136.           if (!$this->db->close()){ 
  137.             $exceptionstring = "Error closing connection: <br />"
  138.             $exceptionstring .= $this->db->errno . ": " . $this->db->error; 
  139.             throw new exception ($exceptionstring); 
  140.           } 
  141.         //Mysql functionality. 
  142.         } else { 
  143.           if (!mysql_close ($this->db)){ 
  144.             $exceptionstring = "Error closing connection: <br />"
  145.             $exceptionstring .= mysql_errno() . ": " . mysql_error(); 
  146.             throw new exception ($exceptionstring); 
  147.           } 
  148.         } 
  149.       } catch (exception $e) { 
  150.         echo $e->getmessage(); 
  151.       } 
  152.     } 
  153.   } 
  154.   //Now, let us create an instance of mydb. 
  155.   $mydb = new mydb ("localhost","root",""); 
  156.   //Select a database to use. 
  157.   $mydb->selectdb ("wps"); 
  158.   //Now, let's perform an action. 
  159.   //$adata = $mydb->execute ("UPDATE cd SET title='Hybrid Theory' WHERE cdid='2'"); 
  160.   //Then, let's try to return a row set. 
  161.   $adata = $mydb->getrows ("SELECT * FROM wp_terms"); 
  162.   for ($i = 0; $i < count ($adata); $i++){ 
  163.     echo $adata[$i] . "<br />"
  164.   } 
  165.   $mydb->selectdb("test"); 
  166.   $result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2"); 
  167.   echo "<br />"
  168.   echo $result; 
  169. ?> 

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

延伸 · 阅读

精彩推荐