class.mysql.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. class Daopdo
  3. {
  4. protected $dbName = '';
  5. protected $dsn;
  6. protected $dbh;
  7. public $result;
  8. public $Record = '';
  9. /** * 构造 * * @return DAOPDO */
  10. public function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
  11. {
  12. try {
  13. $this->dsn = 'mysql:host=' . $dbHost . ';dbname=' . $dbName;
  14. $this->dbh = new PDO($this->dsn, $dbUser, $dbPasswd);
  15. $this->dbh->exec('SET character_set_connection=' . $dbCharset . ', character_set_results=' . $dbCharset . ', character_set_client=binary');
  16. } catch (PDOException $e) {
  17. $this->outputError($e->getMessage());
  18. }
  19. }
  20. /** * 防止克隆 * */
  21. private function __clone()
  22. {
  23. }
  24. /** * Query 查询 *
  25. * @param String $strSql SQL语句
  26. * @param String $queryMode 查询方式(All or Row)
  27. * @param Boolean $debug test test test test test test test test
  28. * @return Array
  29. */
  30. public function query($strSql, $queryMode = 'All', $debug = false)
  31. {
  32. if ($debug === true) $this->debug($strSql);
  33. $recordset = $this->dbh->query($strSql);
  34. $this->getPDOError();
  35. if ($recordset) {
  36. $recordset->setFetchMode(PDO::FETCH_ASSOC);
  37. if ($queryMode == 'All') {
  38. $result = $recordset->fetchAll();
  39. } elseif ($queryMode == 'Row') {
  40. $result = $recordset->fetch();
  41. }
  42. } else {
  43. $result = null;
  44. }
  45. $this->result = $result;
  46. return $result;
  47. }
  48. /**
  49. * 默认遍历
  50. */
  51. public function next_record(){
  52. $this->Record = $this->result;
  53. return 1;
  54. }
  55. /** * Update 更新 *
  56. * @param String $table 表名
  57. * @param Array $arrayDataValue 字段与值
  58. * @param String $where 条件
  59. * @param Boolean $debug
  60. * @return Int
  61. */
  62. public function update($table, $arrayDataValue, $where = '', $debug = false)
  63. {
  64. $this->checkFields($table, $arrayDataValue);
  65. if ($where) {
  66. $strSql = '';
  67. foreach ($arrayDataValue as $key => $value) {
  68. $strSql .= ", `$key`='$value'";
  69. }
  70. $strSql = substr($strSql, 1);
  71. $strSql = "UPDATE `$table` SET $strSql WHERE $where";
  72. } else {
  73. $strSql = "REPLACE INTO `$table` (`" . implode('`,`', array_keys($arrayDataValue)) . "`) VALUES ('" . implode("','", $arrayDataValue) . "')";
  74. }
  75. if ($debug === true) $this->debug($strSql);
  76. $result = $this->dbh->exec($strSql);
  77. $this->getPDOError();
  78. return $result;
  79. }
  80. /** * Insert 插入 *
  81. * @param String $table 表名
  82. * @param Array $arrayDataValue 字段与值 *
  83. * @param Boolean $debug
  84. * @return Int
  85. */
  86. public function insert($table, $arrayDataValue, $debug = false)
  87. {
  88. $this->checkFields($table, $arrayDataValue);
  89. $strSql = "INSERT INTO `$table` (`" . implode('`,`', array_keys($arrayDataValue)) . "`) VALUES ('" . implode("','", $arrayDataValue) . "')";
  90. if ($debug === true) $this->debug($strSql);
  91. $result = $this->dbh->exec($strSql);
  92. $this->getPDOError();
  93. return $result;
  94. }
  95. /** * Replace 覆盖方式插入 *
  96. * @param String $table 表名
  97. * @param Array $arrayDataValue 字段与值
  98. * @param Boolean $debug
  99. * @return Int
  100. */
  101. public function replace($table, $arrayDataValue, $debug = false)
  102. {
  103. $this->checkFields($table, $arrayDataValue);
  104. $strSql = "REPLACE INTO `$table`(`" . implode('`,`', array_keys($arrayDataValue)) . "`) VALUES ('" . implode("','", $arrayDataValue) . "')";
  105. if ($debug === true) $this->debug($strSql);
  106. $result = $this->dbh->exec($strSql);
  107. $this->getPDOError();
  108. return $result;
  109. }
  110. /** * Delete 删除 *
  111. * @param String $table 表名
  112. * @param String $where 条件
  113. * @param Boolean $debug
  114. * @return Int
  115. */
  116. public function delete($table, $where = '', $debug = false)
  117. {
  118. if ($where == '') {
  119. $this->outputError("'WHERE' is Null");
  120. } else {
  121. $strSql = "DELETE FROM `$table` WHERE $where";
  122. if ($debug === true) $this->debug($strSql);
  123. $result = $this->dbh->exec($strSql);
  124. $this->getPDOError();
  125. return $result;
  126. }
  127. }
  128. /** * execSql 执行SQL语句,debug=>true可打印sql调试 *
  129. * @param String $strSql
  130. * @param Boolean $debug
  131. * @return Int
  132. */
  133. public function execSql($strSql, $debug = false)
  134. {
  135. if ($debug === true) $this->debug($strSql);
  136. $result = $this->dbh->exec($strSql);
  137. $this->getPDOError();
  138. return $result;
  139. }
  140. /** * 获取字段最大值 *
  141. * @param string $table 表名
  142. * @param string $field_name 字段名
  143. * @param string $where 条件
  144. */
  145. public function getMaxValue($table, $field_name, $where = '', $debug = false)
  146. {
  147. $strSql = "SELECT MAX(" . $field_name . ") AS MAX_VALUE FROM $table";
  148. if ($where != '') $strSql .= " WHERE $where";
  149. if ($debug === true) $this->debug($strSql);
  150. $arrTemp = $this->query($strSql, 'Row');
  151. $maxValue = $arrTemp["MAX_VALUE"];
  152. if ($maxValue == "" || $maxValue == null) {
  153. $maxValue = 0;
  154. }
  155. return $maxValue;
  156. }
  157. /** * 获取指定列的数量 *
  158. * @param string $table
  159. * * @param string $field_name
  160. * @param string $where
  161. * @param bool $debug
  162. * @return int
  163. */
  164. public function getCount($table, $field_name, $where = '', $debug = false)
  165. {
  166. $strSql = "SELECT COUNT($field_name) AS NUM FROM $table";
  167. if ($where != '') $strSql .= " WHERE $where";
  168. if ($debug === true) $this->debug($strSql);
  169. $arrTemp = $this->query($strSql, 'Row');
  170. return $arrTemp['NUM'];
  171. }
  172. /** * 获取表引擎 *
  173. * @param String $dbName 库名
  174. * @param String $tableName 表名
  175. * @param Boolean $debug
  176. * @return String
  177. */
  178. public function getTableEngine($dbName, $tableName)
  179. {
  180. $strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='".$tableName."'";
  181. $arrayTableInfo = $this->query($strSql);
  182. $this->getPDOError();
  183. return $arrayTableInfo[0]['Engine'];
  184. }
  185. //预处理执行
  186. public function prepareSql($sql=''){
  187. return $this->dbh->prepare($sql);
  188. }
  189. //执行预处理
  190. public function execute($presql){
  191. return $this->dbh->execute($presql);
  192. }
  193. /** * pdo属性设置 */
  194. public function setAttribute($p,$d){
  195. $this->dbh->setAttribute($p,$d);
  196. }
  197. /** * beginTransaction 事务开始
  198. */
  199. public function beginTransaction()
  200. {
  201. $this->dbh->beginTransaction();
  202. }
  203. /** * commit 事务提交 */
  204. public function commit()
  205. {
  206. $this->dbh->commit();
  207. }
  208. /** * rollback 事务回滚 */
  209. public function rollback()
  210. {
  211. $this->dbh->rollback();
  212. }
  213. /** * transaction 通过事务处理多条SQL语句
  214. * 调用前需通过getTableEngine判断表引擎是否支持事务
  215. * * @param array $arraySql
  216. * @return Boolean
  217. */
  218. public function execTransaction($arraySql)
  219. {
  220. $retval = 1;
  221. $this->beginTransaction();
  222. foreach ($arraySql as $strSql) {
  223. if ($this->execSql($strSql) == 0) $retval = 0;
  224. }
  225. if ($retval == 0) {
  226. $this->rollback();
  227. return false;
  228. } else {
  229. $this->commit();
  230. return true;
  231. }
  232. }
  233. /** * checkFields 检查指定字段是否在指定数据表中存在
  234. ** @param String $table
  235. * @param array $arrayField
  236. */
  237. private function checkFields($table, $arrayFields)
  238. {
  239. $fields = $this->getFields($table);
  240. foreach ($arrayFields as $key => $value) {
  241. if (!in_array($key, $fields)) {
  242. $this->outputError("Unknown column `$key` in field list.");
  243. }
  244. }
  245. }
  246. /** * getFields 获取指定数据表中的全部字段名*
  247. * @param String $table 表名
  248. * @return array
  249. */
  250. private function getFields($table)
  251. {
  252. $fields = array();
  253. $recordset = $this->dbh->query("SHOW COLUMNS FROM $table");
  254. $this->getPDOError();
  255. $recordset->setFetchMode(PDO::FETCH_ASSOC);
  256. $result = $recordset->fetchAll();
  257. foreach ($result as $rows) {
  258. $fields[] = $rows['Field'];
  259. }
  260. return $fields;
  261. }
  262. /**
  263. * getPDOError 捕获PDO错误信息
  264. */
  265. private function getPDOError()
  266. {
  267. if ($this->dbh->errorCode() != '00000') {
  268. $arrayError = $this->dbh->errorInfo();
  269. $this->outputError($arrayError[2]);
  270. }
  271. }
  272. /** * debug
  273. ** @param mixed $debuginfo
  274. */
  275. private function debug($debuginfo)
  276. {
  277. var_dump($debuginfo);
  278. exit();
  279. }
  280. /** * 输出错误信息*
  281. * @param String $strErrMsg
  282. */
  283. private function outputError($strErrMsg)
  284. {
  285. throw new Exception('MySQL Error: '.$strErrMsg);
  286. }
  287. /**
  288. * destruct 关闭数据库连接
  289. */
  290. public function destruct()
  291. {
  292. $this->dbh = null;
  293. }
  294. /** *PDO执行sql语句,返回改变的条数
  295. *如需调试可选用execSql($sql,true)
  296. */
  297. public function exec($sql=''){
  298. return $this->dbh->exec($sql);
  299. }
  300. /**
  301. * quote
  302. */
  303. public function quote($str) {
  304. return $this->dbh->quote($str);
  305. }
  306. }