You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

212 lines
5.9 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. $this->my_dbname = TRUE_CONN_DB;
  34. $this->my_port = TRUE_CONN_PORT;
  35. $this->my_host = TRUE_CONN_HOST;
  36. $this->my_user = TRUE_CONN_USER;
  37. $this->my_pwd = TRUE_CONN_PASSWORD;
  38. // if (CONN_DB_NAME == CSTEST_MYSQL_DB) {
  39. // $this->my_port = CSTEST_MYSQL_PORT;
  40. // $this->my_host = CSTEST_MYSQL_HOST;
  41. // $this->my_dbname = CSTEST_MYSQL_DB;
  42. // $this->my_user = CSTEST_MYSQL_USER;
  43. // $this->my_pwd = CSTEST_MYSQL_PASSWORD;
  44. // } else if (CONN_DB_NAME == CS_MYSQL_DB) {
  45. // $this->my_port = CS_MYSQL_PORT;
  46. // $this->my_host = CS_MYSQL_HOST;
  47. // $this->my_dbname = CS_MYSQL_DB;
  48. // $this->my_user = CS_MYSQL_USER;
  49. // $this->my_pwd = CS_MYSQL_PASSWORD;
  50. // } else if (CONN_DB_NAME == WAICE_MYSQL_DB) {
  51. //
  52. // }
  53. $port = $this->my_port ? $this->my_port : 3306;
  54. try {
  55. $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"));
  56. } catch (PDOException $e) {
  57. writeLog('new PDO failed:' . $e->getMessage());
  58. }
  59. }
  60. /**
  61. * 防止被克隆
  62. *
  63. */
  64. private function __clone()
  65. {
  66. }
  67. // single
  68. public static function getInstance()
  69. {
  70. if (FALSE == (self::$_instance instanceof self)) {
  71. self::$_instance = new self();
  72. }
  73. return self::$_instance;
  74. }
  75. // 关闭数据库
  76. function closeDB()
  77. {
  78. $this->my_pdo = null;
  79. }
  80. // 该类销毁时执行
  81. function __destruct()
  82. {
  83. $this->closeDB();
  84. }
  85. // 执行存储过程 返回值是多维数组 eg: $data['rowset']=[[{name:"",age:"18"},{name:"",age:"18"}],[{res_id:"10",res_name:"xxx酒店"},{res_id:"10",res_name:"xxx酒店"}]];
  86. function execProcedure($sql)
  87. {
  88. $result = $this->my_pdo->query($sql);
  89. $data = array();
  90. $temp_data = array();
  91. $data['code'] = "0";
  92. $data['info'] = "";
  93. if (!$result) {
  94. $data['code'] = "2";
  95. $data['info'] = "系统异常";
  96. } else {
  97. do {
  98. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  99. if ($rowset) {
  100. $temp_data[] = $rowset;
  101. } else {
  102. $temp_data[] = array();
  103. }
  104. } while ($result->nextRowset());
  105. $result->closeCursor();
  106. $data['rowset'] = $temp_data;
  107. }
  108. return $data;
  109. }
  110. function execProcedure2($sql)
  111. {
  112. $result = $this->my_pdo->query($sql);
  113. $data = array();
  114. $temp_data = array();
  115. $data['code'] = "0";
  116. $data['info'] = "";
  117. if (!$result) {
  118. $data['code'] = "2";
  119. $data['info'] = "系统异常";
  120. } else {
  121. do {
  122. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  123. if ($rowset && !empty($rowset)) {
  124. if (isset($rowset['errcode'])) {
  125. $temp_data['code'] = $rowset['errcode'];
  126. $temp_data['info'] = $rowset['errinfo'];
  127. } elseif (isset($rowset[0])) {
  128. $temp_data['code'] = $rowset[0]['errcode'];
  129. $temp_data['info'] = $rowset[0]['errinfo'];
  130. }
  131. } else {
  132. $temp_data[] = array();
  133. }
  134. } while ($result->nextRowset());
  135. $result->closeCursor();
  136. $data = $temp_data;
  137. }
  138. return $data;
  139. }
  140. // 执行单行sql语句
  141. function queryBySql($sql)
  142. {
  143. $result = $this->my_pdo->query($sql);
  144. if (!$result) {
  145. $data['code'] = "1";
  146. $data['info'] = "系统异常";
  147. $data['rowset'] = array();
  148. } else {
  149. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  150. $result->closeCursor();
  151. $data['code'] = "0";
  152. $data['info'] = "";
  153. $data['rowset'] = $rowset;
  154. }
  155. return $data;
  156. }
  157. // 执行单条增删改sql语句
  158. function execSql($sql)
  159. {
  160. $result = $this->my_pdo->exec($sql);
  161. if ($result === false) {
  162. $data['code'] = "1";
  163. $data['info'] = "sql error";
  164. } else {
  165. // $result -> closeCursor(); --修改貌似不需要
  166. $data['code'] = "0";
  167. $data['info'] = "";
  168. }
  169. return $data;
  170. }
  171. // 执行增删改多条sql语句
  172. function execSqlArr($sqlArr)
  173. {
  174. $data = array();
  175. $data['code'] = "0";
  176. $data['info'] = "";
  177. try {
  178. $this->my_pdo->beginTransaction();
  179. foreach ($sqlArr as $v) {
  180. $data = $this->execSql($v);
  181. if ($data['code'] != "0") {
  182. throw new PDOException("事务异常");
  183. }
  184. }
  185. $this->my_pdo->commit();
  186. } catch (PDOException $ex) {
  187. $data['code'] = "17";
  188. $data['info'] = "事务异常";
  189. $this->my_pdo->rollBack();
  190. }
  191. return $data;
  192. }
  193. }