Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

205 linhas
5.7 KiB

  1. <?php
  2. //Author:fuhc
  3. //Date:20160906
  4. //连接数据库
  5. //require_once __DIR__.'../config.inc';
  6. require_once __DIR__ . "/../Conf/Config.inc";
  7. require_once __DIR__ . '/Function.php';
  8. //全局PDO在DBUtil.php中
  9. //连接数据库工具类
  10. class DB
  11. {
  12. public $my_port;
  13. public $my_host;
  14. public $my_dbname;
  15. public $my_user;
  16. public $my_pwd;
  17. public $my_pdo;
  18. static $_instance;
  19. //存储对象
  20. // function __construct($type){
  21. // $this->my_port.=$type.$this->my_port;
  22. // $this->my_host=$type.$this->my_host;
  23. // $this->my_dbname=$dbname;
  24. // $this->my_user=$user;
  25. // $this->my_pwd=$pwd;
  26. // }
  27. /**
  28. * 构造函数
  29. * 私有
  30. */
  31. private function __construct()
  32. {
  33. if (CONN_DB_NAME == CSTEST_MYSQL_DB) {
  34. $this->my_port = CSTEST_MYSQL_PORT;
  35. $this->my_host = CSTEST_MYSQL_HOST;
  36. $this->my_dbname = CSTEST_MYSQL_DB;
  37. $this->my_user = CSTEST_MYSQL_USER;
  38. $this->my_pwd = CSTEST_MYSQL_PASSWORD;
  39. } else if (CONN_DB_NAME == CS_MYSQL_DB) {
  40. $this->my_port = CS_MYSQL_PORT;
  41. $this->my_host = CS_MYSQL_HOST;
  42. $this->my_dbname = CS_MYSQL_DB;
  43. $this->my_user = CS_MYSQL_USER;
  44. $this->my_pwd = CS_MYSQL_PASSWORD;
  45. } else if (CONN_DB_NAME == WAICE_MYSQL_DB) {
  46. }
  47. $port = $this->my_port ? $this->my_port : 3306;
  48. try {
  49. $this->my_pdo = new PDO("mysql:host=" . $this->my_host . ";port=" . $port . ";dbname=" . $this->my_dbname, $this->my_user, $this->my_pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
  50. } catch (PDOException $e) {
  51. writeLog('new PDO failed:' . $e->getMessage());
  52. }
  53. }
  54. /**
  55. * 防止被克隆
  56. *
  57. */
  58. private function __clone()
  59. {
  60. }
  61. // single
  62. public static function getInstance()
  63. {
  64. if (FALSE == (self::$_instance instanceof self)) {
  65. self::$_instance = new self();
  66. }
  67. return self::$_instance;
  68. }
  69. // 关闭数据库
  70. function closeDB()
  71. {
  72. $this->my_pdo = null;
  73. }
  74. // 该类销毁时执行
  75. function __destruct()
  76. {
  77. $this->closeDB();
  78. }
  79. // 执行存储过程 返回值是多维数组 eg: $data['rowset']=[[{name:"",age:"18"},{name:"",age:"18"}],[{res_id:"10",res_name:"xxx酒店"},{res_id:"10",res_name:"xxx酒店"}]];
  80. function execProcedure($sql)
  81. {
  82. $result = $this->my_pdo->query($sql);
  83. $data = array();
  84. $temp_data = array();
  85. $data['code'] = "0";
  86. $data['info'] = "";
  87. if (!$result) {
  88. $data['code'] = "2";
  89. $data['info'] = "系统异常";
  90. } else {
  91. do {
  92. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  93. if ($rowset) {
  94. $temp_data[] = $rowset;
  95. } else {
  96. $temp_data[] = array();
  97. }
  98. } while ($result->nextRowset());
  99. $result->closeCursor();
  100. $data['rowset'] = $temp_data;
  101. }
  102. return $data;
  103. }
  104. function execProcedure2($sql)
  105. {
  106. $result = $this->my_pdo->query($sql);
  107. $data = array();
  108. $temp_data = array();
  109. $data['code'] = "0";
  110. $data['info'] = "";
  111. if (!$result) {
  112. $data['code'] = "2";
  113. $data['info'] = "系统异常";
  114. } else {
  115. do {
  116. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  117. if ($rowset && !empty($rowset)) {
  118. if (isset($rowset['errcode'])) {
  119. $temp_data['code'] = $rowset['errcode'];
  120. $temp_data['info'] = $rowset['errinfo'];
  121. } elseif (isset($rowset[0])) {
  122. $temp_data['code'] = $rowset[0]['errcode'];
  123. $temp_data['info'] = $rowset[0]['errinfo'];
  124. }
  125. } else {
  126. $temp_data[] = array();
  127. }
  128. } while ($result->nextRowset());
  129. $result->closeCursor();
  130. $data = $temp_data;
  131. }
  132. return $data;
  133. }
  134. // 执行单行sql语句
  135. function queryBySql($sql)
  136. {
  137. $result = $this->my_pdo->query($sql);
  138. if (!$result) {
  139. $data['code'] = "1";
  140. $data['info'] = "系统异常";
  141. $data['rowset'] = array();
  142. } else {
  143. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  144. $result->closeCursor();
  145. $data['code'] = "0";
  146. $data['info'] = "";
  147. $data['rowset'] = $rowset;
  148. }
  149. return $data;
  150. }
  151. // 执行单条增删改sql语句
  152. function execSql($sql)
  153. {
  154. $result = $this->my_pdo->exec($sql);
  155. if ($result === false) {
  156. $data['code'] = "1";
  157. $data['info'] = "sql error";
  158. } else {
  159. // $result -> closeCursor(); --修改貌似不需要
  160. $data['code'] = "0";
  161. $data['info'] = "";
  162. }
  163. return $data;
  164. }
  165. // 执行增删改多条sql语句
  166. function execSqlArr($sqlArr)
  167. {
  168. $data = array();
  169. $data['code'] = "0";
  170. $data['info'] = "";
  171. try {
  172. $this->my_pdo->beginTransaction();
  173. foreach ($sqlArr as $v) {
  174. $data = $this->execSql($v);
  175. if ($data['code'] != "0") {
  176. throw new PDOException("事务异常");
  177. }
  178. }
  179. $this->my_pdo->commit();
  180. } catch (PDOException $ex) {
  181. $data['code'] = "17";
  182. $data['info'] = "事务异常";
  183. $this->my_pdo->rollBack();
  184. }
  185. return $data;
  186. }
  187. }