php封装操作数据库的类

  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. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值