php封装操作数据库的类

[php]  view plain copy
  1. ConnDB.class.inc.php文件  
  2.   
  3. <?php  
  4. // 连接数据库的类  
  5. class ConnDB  
  6. {  
  7.         var $dbtype;  
  8.         var $host;  
  9.         var $usr;  
  10.         var $pwd;  
  11.         var $dbname;  
  12.         var $conn;  
  13.         // 构造方法  
  14.         function ConnDB($dbtype,$host,$user,$pwd,$dbname)  
  15.         {  
  16.                 $this->dbtype = $dbtype;  
  17.                 $this->host = $host;  
  18.                 $this->pwd = $pwd;  
  19.                 $this->dbname = $dbname;  
  20.                 $this->user = $user;  
  21.         }  
  22.         function GetConn()  
  23.         {  
  24.             $this->conn = mysql_connect($this->host,$this->user,$this->pwd) or die("数据库服务器连接错误".mysql_error());   
  25.             mysql_select_db($this->dbname,$this->conn) or die("数据库访问错误".mysql_error());  
  26.             mysql_query("set names gb2312");// 设置编码格式  
  27.             return $this->conn;  
  28.         }  
  29.         function __destruct()  
  30.         {  
  31.             $this->CloseDB();      
  32.         }  
  33.         function CloseDB()  
  34.         {  
  35.             mysql_close($this->conn);  
  36.         }  
  37.           
  38. }  
  39. // 数据库操作类  
  40. class OperateDB  
  41. {  
  42.     function Execsql($sql,$conn)  
  43.     {  
  44.             $sqltype = strtolower(substr(trim($sql),0,6));// 截取sql语句中的前6个字符串,并转换成小写  
  45.             $result = mysql_query($sql,$conn);// 执行sql语句  
  46.             $calback_arrary = array();// 定义二维数组  
  47.             if ("select" == $sqltype)// 判断执行的是select语句  
  48.             {  
  49.                   
  50.                 if (false == $result)  
  51.                 {  
  52.                     return false;     
  53.                 }  
  54.                 else if (0 == mysql_num_rows($result))  
  55.                 {  
  56.                     return false;  
  57.                 }  
  58.                 else  
  59.                 {  
  60.                     while($result_array = mysql_fetch_array($result))  
  61.                     {  
  62.                     array_push($calback_arrary$result_array);  
  63.                     }  
  64.                     return $calback_arrary;// 成功返回查询结果的数组     
  65.                 }  
  66.             }  
  67.             else if ("update" == $sqltype || "insert" == $sqltype || "delete" == $sqltype)  
  68.             {  
  69.                     if ($result)  
  70.                     {  
  71.                         return true;  
  72.                     }  
  73.                     else  
  74.                     {  
  75.                         return false;  
  76.                     }  
  77.             }  
  78.     }     
  79. }  
  80.   
  81. ?>  

[php]  view plain copy
  1. // conndb.inc.php文件  
  2. <?php  
  3. require("ConnDB.class.inc.php");  
  4. $ccon = new ConnDB("mysql","localhost","root","root","think_cms");  
  5. $operatedb = new OperateDB();  
  6. $conn = $ccon->GetConn();  
  7.   
  8. ?>  

用法:

[php]  view plain copy
  1. <?php  
  2. require("conndb.inc.php");  
  3. $result = $operatedb->Execsql("select * from cms_post where user_id=2",$conn);  
  4. foreach($result as $show)  
  5. {  
  6.     echo $show['post_body'].'<br>';     
  7. }  
  8. ?>  



http://www.oschina.net/code/snippet_591011_47258

你可能感兴趣的:(php封装操作数据库的类)