免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2069 | 回复: 0
打印 上一主题 下一主题

剛寫了一個pdo的class大家可否給點意見? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-19 10:20 |只看该作者 |倒序浏览
剛寫了一個pdo的class大家可否給點意見?有沒有可改善的地方?


  1. <?php

  2. class Database{
  3.         var $host;
  4.         var $userName;
  5.         var $userPwd;
  6.         var $port='3306';
  7.         var $name;
  8.         var $pconnect=true;
  9.         var $charset='UTF-8';
  10.         var $encoding='UTF-8';
  11.                
  12.         var $pdo;
  13.         var $result;
  14.         var $format='record';
  15.        
  16.         public function __construct($db){
  17.                 $this->host=$db['host'];
  18.                 $this->userName=$db['userName'];
  19.                 $this->userPwd=$db['userPwd'];
  20.                 $this->port=$db['port']!=''?$db['port']:$this->port;
  21.                 $this->name=$db['name'];
  22.                 $this->pconnect=$db['pconnect']!=''?$db['pconnect']:$this->pconnect;
  23.                 $this->charset=$db['charset']!=''?$db['charset']:$this->charset;
  24.                 $this->encoding=$db['encoding']!=''?$db['encoding']:$this->encoding;
  25.         }
  26.        
  27.         public function connect(){
  28.                 try{
  29.                         $database="mysql:host={$this->host};port={$this->port};dbname={$this->name}";
  30.                         $this->pdo = new PDO($database, $this->userName, $this->userPwd, array(PDO::ATTR_PERSISTENT => $this->pconnect));
  31.                         //dbLog($database,'debug',$db,$xml);
  32.                         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33.                         $this->pdo->setAttribute(PDO::ATTR_TIMEOUT,5);
  34.                         return $this->pdo;
  35.                 } catch (PDOException $e) {
  36.                         throw $e;
  37.                 }
  38.         }
  39.        
  40.         function setNames(){
  41.                 $str="SET NAMES '{$this->charset}'";
  42.                 $this->pdo->exec($str);
  43.         }
  44.        
  45.         function exec($sql){
  46.                 try{
  47.                         //dbLog($sql,'sql',$db,$xml);
  48.                         return $this->pdo->exec($sql);
  49.                 } catch (PDOException $e) {
  50.                   throw $e;
  51.                 }
  52.         }
  53.        
  54.         function query($sql){
  55.                 try{
  56.                         //dbLog($sql,'sql',$db,$xml);
  57.                   return $this->pdo->query($sql);
  58.                 } catch (PDOException $e) {
  59.                   throw $e;
  60.                 }
  61.         }

  62.         function setFormat($format){
  63.                 $format=strtolower($format);
  64.                 if (in_array($format,array('xml','json','record'))){
  65.                         $this->format=$format;
  66.                 } else {
  67.                         echo 'wrong format';
  68.                 }
  69.         }

  70.         function exception($e){
  71.         if ($GLOBALS['cfg']['debug']){
  72.                         if ($this->format=='xml'){
  73.                                 $root = $xml->getElementsByTagName('rows')->item(0);
  74.                                 $exception = $xml->createElement("exception");
  75.                                 $root->appendChild($exception);
  76.                                        
  77.                                 $msg = $xml->createElement("msg");
  78.                                 $exception->appendChild($msg);
  79.                                 $value = $xml->createTextNode(dbParseData($e->getMessage(),$db));
  80.                                 $msg->appendChild($value);
  81.                        
  82.                                 $file = $xml->createElement("file");
  83.                                 $exception->appendChild($file);
  84.                                 $value = $xml->createTextNode(dbParseData($e->getFile(),$db));
  85.                                 $file->appendChild($value);
  86.                        
  87.                                 $line = $xml->createElement("line");
  88.                                 $exception->appendChild($line);
  89.                                 $value = $xml->createTextNode(dbParseData($e->getLine(),$db));
  90.                                 $line->appendChild($value);
  91.                                
  92.                                 $trace = $xml->createElement("trace");
  93.                                 $exception->appendChild($trace);
  94.                                 $value = $xml->createTextNode(dbParseData($e->getTraceAsString(),$db));
  95.                                 $trace->appendChild($value);
  96.                         } else if ($this->format=='json'){
  97.                                
  98.                                         $json=array(
  99.                                                 'Message'=>$e->getMessage(),
  100.                                         'File'=>$e->getFile(),
  101.                                         'Line'=>$e->getLine(),
  102.                                         'Trace'=>$e->getTraceAsString()
  103.                                 );
  104.                                 responseJson($json);
  105.                                 die();
  106.                         } else if ($this->format=='record'){
  107.                                 echo "<p>";
  108.                                 echo "Message : " . $e->getMessage() . "<br/>";
  109.                                 echo "File : " . $e->getFile() . "<br/>";
  110.                                 echo "Line : " . $e->getLine() . "<br/>";
  111.                                 echo "Trace : " . $e->getTraceAsString() . "<br/>";
  112.                                 echo "</p>";
  113.                                 die();
  114.                         }  
  115.         }
  116.         }
  117. }


  118. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP