大年初七,在家无聊,写了个mysql操作类

浏览量:471 | 分类:PHP | 发布日期:2010-02-20

在家无聊,写了个简单的DB类,方便以后CURD;
以前用公司现成的DB类,现在自己写个;
支持Mysqli,高手见笑,本人还是菜鸟 ;
 

代码如下
  1. <?php
  2.  
  3. class MysqlDB {
  4.  
  5.     private $hostname;
  6.     private $username;
  7.     private $password;
  8.     private $db;
  9.  
  10.     public function __construct() {
  11.         $num_args = func_num_args();
  12.         if($num_args > 0) {
  13.             $args = func_get_args();
  14.             $this->host = $args[0];
  15.             $this->user = $args[1];
  16.             $this->pass = $args[2];
  17.         }
  18.         $this->content();
  19.     }
  20.  
  21.     private function mysqli_installed(){
  22.         if (function_exists ("mysqli_connect")){
  23.             return true;
  24.         } else {
  25.             return false;
  26.         }
  27.     }
  28.  
  29.     private function content() {
  30.         try{
  31.          if($this->mysqli_installed()) {
  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.          }else{
  38.             if(!$this->db = new mysql_connect($this->host,$this->user,$this->pass)) {
  39.                 $exceptionstring = "Error connection to database: <br />";
  40.                 $exceptionstring .= mysql_errno() . ": " . mysql_error();
  41.                 throw new exception ($exceptionstring);
  42.             }
  43.          }
  44.         } catch (exception $e) {
  45.             echo $e->getmessage();
  46.         }
  47.     }
  48.  
  49.     public function select_db($db_name) {
  50.         try{
  51.             if($this->mysqli_installed() ) {
  52.                 if(!$this->db->select_db($db_name)) {
  53.                     $exceptionstring = "Error opening database: $db_name: <br />";
  54.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  55.                     throw new exception ($exceptionstring);
  56.                 }
  57.             }else{
  58.                 if(!mysql_select_db($db_name,$this->db)) {
  59.                     $exceptionstring = "Error opening database: $db_name: <br />";
  60.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  61.                     throw new exception ($exceptionstring);
  62.                 }
  63.             }
  64.         } catch (exception $e) {
  65.             echo $e->getmessage();
  66.         }
  67.     }
  68.  
  69.     public function execute($query) {
  70.         try{
  71.             if($this->mysqli_installed()) {
  72.                 if(!$this->db->query($query)) {
  73.                     $exceptionstring = "Error performing query: $query: <br />";
  74.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  75.                     throw new exception ($exceptionstring);
  76.                 }
  77.             }else{
  78.                 if(!mysql_query($query,$this->db)) {
  79.                     $exceptionstring = "Error performing query: $query: <br />";
  80.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  81.                     throw new exception ($exceptionstring);
  82.                 }else{
  83.                     echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
  84.                 }
  85.             }
  86.         } catch (exception $e) {
  87.             echo $e->getmessage();
  88.         }
  89.     }
  90.  
  91.     public function get_rows ($query){
  92.         try {
  93.             if ($this->mysqli_installed()){
  94.                 if ($result = $this->db->query ($query)){
  95.                     $returnarr = array ();
  96.                     while ($adata = $result->fetch_array ()){
  97.                         $returnarr = array_merge ($returnarr,$adata);
  98.                     }
  99.                     return $returnarr;
  100.                 } else {
  101.                     $exceptionstring = "Error performing query: $query: <br />";
  102.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  103.                     throw new exception ($exceptionstring);
  104.                 }
  105.             } else {
  106.                 if (!$aquery = mysql_query ($query)){
  107.                     $exceptionstring = "Error performing query: $query: <br />";
  108.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  109.                     throw new exception ($exceptionstring);
  110.                 } else {
  111.                     $returnarr = array ();
  112.                     while ($adata = mysql_fetch_array ($aquery)){
  113.                         $returnarr = array_merge ($returnarr,$adata);
  114.                     }
  115.                     return $returnarr;
  116.                 }
  117.             }
  118.         } catch (exception $e) {
  119.             echo $e->getmessage();
  120.         }
  121.     }
  122.  
  123.     public function affected_rows() {
  124.         if($this->mysqli_installed()) {
  125.             return $this->db->affected_rows;
  126.         }else{
  127.             return mysql_affected_rows ($this->db);
  128.         }
  129.     }
  130.  
  131.     public function insert_id() {
  132.         if($this->mysqli_installed()) {
  133.             return $this->db->insert_id;
  134.         }else{
  135.             return mysql_insert_id ($this->db);
  136.         }
  137.     }
  138.  
  139.     public function escape($str){
  140.         return trim(mysql_escape_string($str));
  141.     }
  142.  
  143.     public function __destruct() {
  144.         try {
  145.             if ($this->mysqli_installed()){
  146.                 if (!$this->db->close()){
  147.                     $exceptionstring = "Error closing connection: <br />";
  148.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  149.                     throw new exception ($exceptionstring);
  150.                 }
  151.             } else {
  152.                 if (!mysql_close ($this->db)){
  153.                     $exceptionstring = "Error closing connection: <br />";
  154.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  155.                     throw new exception ($exceptionstring);
  156.                 }
  157.             }
  158.         } catch (exception $e) {
  159.             echo $e->getmessage();
  160.         }
  161.     }
  162.  
  163. }
  164.  
  165.  
  166. /*test*/
  167. $mysql = new MysqlDB('localhost','root','');
  168.  
  169. $mysql->select_db('test');
  170.  
  171. $update = $mysql->execute ("UPDATE test SET usereMail='test@test.com' WHERE id='1'");
  172.  
  173. $insert = $mysql->execute ("INSERT INTO test SET usereMail = 'test@test.com'");
  174.  
  175. $insert_id = $mysql->insert_id($insert);
  176.  
  177. $test = $mysql->get_rows("SELECT * FROM `test` WHERE 1");
  178.  
  179. $affected_rows = $mysql->affected_rows($insert);
  180.  
  181.  
  182. ?>

上一篇: 大年初二

下一篇: 最简单的PHP模板引擎

评论

过客   2010-02-21 00:00:43

深藏不露啊,你牛逼!我服你了