mysqldb.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. class Mysqldb
  3. {
  4. private $dbh;
  5. private $statement;
  6. private $dbconfig;
  7. function __construct($dbKey = array())
  8. {
  9. if (count($dbKey) <= ZERO) {
  10. $this->dbconfig = array
  11. (
  12. 'host' => DBHOST,
  13. 'port' => DBPORT,
  14. 'user' => DBUSER,
  15. 'pass' => DBPASSWORD,
  16. 'dbname' => DBNAME
  17. );
  18. } else {
  19. log_message::info("############", json_encode($dbKey));
  20. $this->dbconfig = array
  21. (
  22. 'host' => $dbKey['host'],
  23. 'port' => 3306,
  24. 'user' => $dbKey['user'],
  25. 'pass' => $dbKey['pass'],
  26. 'dbname' => $dbKey['dbname']
  27. );
  28. log_message::info("############", json_encode($this->dbconfig));
  29. }
  30. //$this->dbconfig = $dbconfig;
  31. $dbh = $this->connect();
  32. if ($this->connect()) {
  33. $this->dbh = $this->connect();
  34. }
  35. }
  36. private function connect()
  37. {
  38. $_dsn = 'mysql:host=' . $this->dbconfig['host'] . ';';
  39. $_dsn .= 'port=' . $this->dbconfig['port'] . ';';
  40. $_dsn .= 'dbname=' . $this->dbconfig['dbname'] . ';';
  41. $_dsn .= 'charset=utf8';
  42. log_message::info('Connect : ' . $_dsn);
  43. try {
  44. $dbLink = new PDO($_dsn, $this->dbconfig['user'],
  45. $this->dbconfig['pass'], array(
  46. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
  47. $dbLink->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  48. } catch (PDOException $e) {
  49. log_message::info('Connection failed: ' . $e->getMessage(), 'db');
  50. return false;
  51. }
  52. return $dbLink;
  53. }
  54. public function close()
  55. {
  56. if (is_object($this->dbh)) {
  57. $this->dbh = null;
  58. }
  59. }
  60. public function query($sql, $prepare = array())
  61. {
  62. if (empty($sql) || !is_array($prepare) || empty($this->dbh)) { //echo "lod false prepare 1";
  63. log_message::info("false execution failed", 'db');
  64. // 重新实例化
  65. // $msyql = new Mysqldb();
  66. }
  67. //log_message::info("sqll" . $sql);
  68. //echo "lod false prepare ??";
  69. $dbh = $this->dbh->prepare($sql);
  70. //echo "lod false prepare ??";
  71. $dbh->closeCursor();
  72. $res = $dbh->execute($prepare);
  73. //echo "lod false prepare ??";
  74. if (!$res) {
  75. log_message::info("db error : " . json_encode($dbh->errorInfo()) . " sql:" . $sql, 'db');
  76. return false;
  77. }
  78. $this->statement = $dbh;
  79. return $dbh;
  80. }
  81. /**
  82. * update
  83. *
  84. */
  85. public function update($table, $aFields, $where = '', $prepare_array = array())
  86. {
  87. if (!$where)
  88. return false;
  89. if (!is_array($aFields) || count($aFields) < 1)
  90. return false;
  91. $aSet = array();
  92. foreach ($aFields as $key => $v) {
  93. $aSet[] = "`{$key}`=:{$key}";
  94. $prepare_array[$key] = $v;
  95. }
  96. $aSet && $set = implode(',', $aSet);
  97. if (empty($set))
  98. return false;
  99. $sql = " UPDATE {$table} SET {$set} WHERE {$where}";
  100. if ($this->query($sql, $prepare_array)) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * 自定义 update
  107. *
  108. *
  109. * public function update2($table, $aFields, $where = array())
  110. * {
  111. * if (!$where)
  112. * return false;
  113. *
  114. * if (!is_array($aFields) || count($aFields) < 1)
  115. * return false;
  116. *
  117. * $aSet = array();
  118. * foreach ($aFields as $key => $v) {
  119. * $aSet[] = "`{$key}`={$v}";
  120. * }
  121. *
  122. * foreach ($where as $fileKey => $fileVar) {
  123. * if (isset($whereOut)) {
  124. * $whereOut[$fileKey] = "AND {$fileKey}={$fileVar}";
  125. * } else {
  126. * $whereOut[$fileKey] = "WHERE {$fileKey}={$fileVar}";
  127. * }
  128. * }
  129. * $aSet && $set = implode(',', $aSet);
  130. *
  131. * $wheres = implode(' ', $whereOut);
  132. *
  133. * $sql = " UPDATE {$table} SET {$set} {$wheres}";
  134. * //_log("updata2 sql ".$sql,'db');
  135. * if ($this->mysqlQuery($sql)) {
  136. * return true;
  137. * }
  138. * return false;
  139. *
  140. * } */
  141. public function update2($table, $aFields, $where = '', $prepare_array = array())
  142. {
  143. if (!$where)
  144. return false;
  145. if (!is_array($aFields) || count($aFields) < 1)
  146. return false;
  147. $aSet = array();
  148. foreach ($aFields as $key => $v) {
  149. $aSet[] = "`{$key}`=:{$key}";
  150. $prepare_array[$key] = $v;
  151. }
  152. $aSet && $set = implode(',', $aSet);
  153. if (empty($set))
  154. return false;
  155. $sql = " UPDATE {$table} SET {$set} WHERE {$where}";
  156. if ($this->query($sql, $prepare_array)) {
  157. if ($this->rowcount() > 0) {
  158. return true;
  159. }
  160. return true;
  161. }
  162. return false;
  163. }
  164. public function mysqlQuery($sql)
  165. {
  166. if (empty($sql) || empty($this->dbh)) {
  167. return false;
  168. log_message::info("false execution failed", 'db');
  169. }
  170. return $this->dbh->query($sql);
  171. }
  172. /**
  173. * 返回结果集
  174. * @$statement PDO
  175. */
  176. public function fetch_all($statement = false)
  177. {
  178. if (!isset($statement) || empty($statement)) {
  179. return $this->statement->fetchAll(PDO::FETCH_ASSOC);
  180. }
  181. return $statement->fetchAll(PDO::FETCH_ASSOC);
  182. }
  183. public function rowcount($statement = false)
  184. {
  185. if (!isset($statement) || empty($statement)) {
  186. return $this->statement->rowCount();
  187. }
  188. return $statement->rowCount();
  189. }
  190. /**
  191. * 取得单行记录
  192. *
  193. * @return array
  194. */
  195. public function fetch_row($statement = false)
  196. {
  197. if (!isset($statement) || empty($statement)) {
  198. return $this->statement->fetch(PDO::FETCH_ASSOC);
  199. }
  200. return $statement->fetch(PDO::FETCH_ASSOC);
  201. }
  202. /**
  203. * 单个录入
  204. *
  205. * @param type $table tables
  206. * @param type $array =array(fields=>value)
  207. * @param type $dbLink pdo connect
  208. */
  209. public function insert($table, $array = array())
  210. {
  211. $sql = " INSERT INTO {$table} ";
  212. $fields = array_keys($array);
  213. $values = array_values($array);
  214. $condition = array_fill(1, count($fields), '?');
  215. $sql .= "(`" . implode('`,`', $fields) . "`)
  216. VALUES (" . implode(',', $condition) . ")";
  217. log_message::info("asdasdsa" . $sql);
  218. return $this->query($sql, $values);
  219. }
  220. public function insert2($table, $array = array(), $addition = null)
  221. {
  222. $sql = " INSERT INTO {$table} ";
  223. $fields = array_keys($array);
  224. $values = array_values($array);
  225. $condition = array_fill(1, count($fields), '?');
  226. $sql .= "(`" . implode('`,`', $fields) . "`)
  227. VALUES (" . implode(',', $condition) . ") " . $addition;
  228. return $this->query($sql, $values);
  229. }
  230. /**
  231. * 批量插入
  232. * 字段与数值的对应关系,请调用方处理好
  233. *
  234. * @param type $table
  235. * @param type $field = "uid,name,sex",用逗号隔开
  236. * @param type $data = 多维数组
  237. */
  238. public function insertBatch($table, $fields, $data = array())
  239. {
  240. //log_message::info(" 批量录入 insertBatch");
  241. if (empty($fields))
  242. return false;
  243. if (!is_array($data) || count($data) < 1) {
  244. return false;
  245. }
  246. $sql = " INSERT INTO {$table} ($fields) VALUES ";
  247. foreach ($data as $v) {
  248. $sql .= "(" . implode(',', $v) . "),";
  249. }
  250. //log_message::info($sql);
  251. return $this->query(rtrim($sql, ','), array());
  252. }
  253. /**
  254. * 注入符验证
  255. *
  256. * @param type $sql_str 验证字符
  257. */
  258. private function inject_check($sql_str)
  259. {
  260. $injectSign = 'select|insert|update|delete|\'|\/\*|\*|';
  261. $injectSign .= '\.\.\/|\.\/|union|into|load_file|outfile';
  262. $check = eregi($injectSign, $sql_str);
  263. if ($check) {
  264. //exit('{"status":"false","result":"Invaild symbol"}');
  265. } else {
  266. return $sql_str;
  267. }
  268. }
  269. /***
  270. * @param $data
  271. * @param null $addition
  272. * @return bool|string
  273. */
  274. public static function formatSqlWhere($data, $addition = null)
  275. {
  276. if (isDatas($data)) {
  277. $where = null;
  278. foreach ($data as $field_key => $field_val) {
  279. if (!empty($where)) {
  280. $where .= ' AND ' . $field_key . '=:' . $field_key;
  281. } else {
  282. $where .= $field_key . '=:' . $field_key;
  283. }
  284. }
  285. log_message::info("formatSqlWhere data is ok", $where, $addition);
  286. return $where . ' ' . $addition;
  287. }
  288. log_message::info("formatSqlWhere data is null", FAILURE);
  289. return false;
  290. }
  291. }
  292. ?>