Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. <?php
  2. class HandleMysql{
  3. /*对数据库增删改查*/
  4. private $dbHost; //数据库地址
  5. private $dbUser; //用户名
  6. private $dbPassword; //密码
  7. private $dbName; //数据库
  8. private $dbConn; //数据库连接
  9. private $result; //结果集
  10. private $sql; //sql语句
  11. private $coding; //编码方式
  12. private $queryNum; //查询次数
  13.  
  14. /**
  15. *
  16. * @param [type] $dbHost [数据库地址]
  17. * @param [type] $dbUser [用户名]
  18. * @param [type] $dbPassword [密码]
  19. * @param [type] $dbName [数据表]
  20. */
  21. public function __construct($dbHost, $dbUser, $dbPassword, $dbName, $coding) {
  22. $this->dbHost = $dbHost;
  23. $this->dbUser = $dbUser;
  24. $this->dbPassword = $dbPassword;
  25. $this->dbName = $dbName;
  26. $this->coding = $coding;
  27. $this->connect();
  28. $this->select_db($dbName);
  29. }
  30.  
  31.  
  32. /**
  33. * 连接数据库
  34. * @return [type] [数据库连接]
  35. */
  36. public function connect() {
  37.  
  38. $this->dbConn = mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword);
  39. if (!$this->dbConn) {
  40. $this->halt("不能连接数据库", $this->sql);
  41. return false;
  42. }
  43.  
  44. if ($this->version() > '4.1') {
  45. $serverset = $this->coding ? "character_set_connection='$this->coding',character_set_results='$this->coding',character_set_client=binary" : '';
  46. $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',') . " sql_mode='' ") : '';
  47. $serverset && mysql_query("SET $serverset", $this->dbConn);
  48. }
  49.  
  50. return $this->dbConn;
  51. }
  52.  
  53. /**
  54. * 选择数据库
  55. * @param [type] $dbName [选择到的数据库]
  56. * @return [type] [是否选择成功]
  57. */
  58. public function select_db($dbName) {
  59. if (!@mysql_select_db($dbName, $this->dbConn)) {
  60. $this->halt("没有" . $dbName . "这个数据库");
  61. return false;
  62. } else {
  63. $this->dbName = $dbName;
  64. return true;
  65. }
  66. }
  67.  
  68.  
  69. /**
  70. * 查询数据库
  71. * @param [type] $sql [查询语句]
  72. * @return [type] [结果集]
  73. */
  74. public function query($sql) {
  75.  
  76. if ($query = mysql_query($sql, $this->dbConn)) {
  77. $this->queryNum++;
  78. return $query;
  79. } else {
  80. $this->halt("Mysql 查询出错", $sql);
  81. return false;
  82. }
  83. }
  84.  
  85. /**
  86. * 插入一组或一条数据
  87. * @param [type] $tableName [数据表名]
  88. * @param [type] $info [数据]
  89. * @return [type] [description]
  90. */
  91. public function insert($tableName, $info) {
  92.  
  93. $this->checkFields($tableName, $info);
  94. $insert_sql = "INSERT INTO `$tableName`(`" . implode('`,`', array_keys($info)) . "`) VALUES('" . implode("','", $info) . "')";
  95. return $this->query("$insert_sql");
  96. }
  97.  
  98. /**
  99. * 更新数据表中的信息
  100. * @param [type] $tableName [数据表名]
  101. * @param [type] $info [数据]
  102. * @param string $where [插入位置]
  103. * @return [type] [description]
  104. */
  105. public function update($tableName, $info, $where = '') {
  106. $this->checkFields($tableName, $info);
  107. if ($where) {
  108. $sql = '';
  109.  
  110. foreach ($info as $k => $v) {
  111. $sql .= ", `$k`='$v'";
  112. }
  113. $sql = substr($sql, 1);
  114.  
  115. $sql = "UPDATE `$tableName` SET $sql WHERE $where";
  116. } else {
  117. $sql = "REPLACE INTO `$tableName`(`" . implode('`,`', array_keys($info)) . "`) VALUES('" . implode("','", $info) . "')";
  118. }
  119. return $this->query($sql);
  120. }
  121.  
  122. /**
  123. * 检查一个字段是否在这张表中存在
  124. *
  125. * @param string $tableName
  126. * @param array $array
  127. * @return message
  128. */
  129. public function checkFields($tableName, $array) {
  130.  
  131. $fields = $this->getFields($tableName);
  132.  
  133. foreach ($array as $key => $val) {
  134. if (!in_array($key, $fields)) {
  135. $this->halt("Mysql 错误", "找不到" . $key . "这个字段在" . $tableName . "里面");
  136. return false;
  137. }
  138. }
  139. }
  140.  
  141.  
  142. /**
  143. * 获取一张表中的所有字段
  144. * @param [type] $tableName [description]
  145. * @return [type] [description]
  146. */
  147. public function getFields($tableName) {
  148. $fields = array();
  149. $result = $this->query("SHOW COLUMNS FROM `$tableName`");
  150. while ($list = $this->fetchArray($result)) {
  151. $fields[] = $list['Field'];
  152. }
  153. $this->freeResult($result);
  154. return $fields;
  155. }
  156.  
  157.  
  158. /**
  159. *
  160. * 释放当前数据库结果集的内存
  161. * @param [type] $result [description]
  162. * @return [type] [description]
  163. */
  164. public function freeResult($result) {
  165. return @mysql_free_result($result);
  166. }
  167.  
  168. /**
  169. * 使用while 可以迭代输出 一个结果集中的所有数据,转化为关联数组
  170. * @param [type] $query [description]
  171. * @param [type] $result_type [description]
  172. * @return [type] [description]
  173. */
  174. public function fetchArray($query, $result_type = MYSQL_ASSOC) {
  175. return mysql_fetch_array($query, $result_type);
  176. }
  177.  
  178. /**
  179. * 返回一个结果集中的一条数据
  180. * @param [type] $sql [description]
  181. * @param string $type [description]
  182. * @param integer $expires [description]
  183. * @return [type] [description]
  184. */
  185. public function getOne($sql, $expires = 3600) {
  186. $query = $this->query($sql, $expires);
  187. if (!is_bool($query)) {
  188. $rs = $this->fetchArray($query);
  189. $this->freeResult($rs);
  190. return $rs;
  191. }else{
  192. return false;
  193. }
  194. }
  195.  
  196. /**
  197. * 返回插入数据的insertid
  198. * @return [type] [description]
  199. */
  200. public function insertId() {
  201. return mysql_insert_id($this->dbConn);
  202. }
  203.  
  204. /**
  205. * 获取当前的结果集中存在多少条数据
  206. * @param [type] $query [description]
  207. * @return [type] [description]
  208. */
  209. public function numRows($query) {
  210. return @mysql_numrows($query);
  211. }
  212.  
  213. /**
  214. * 获取当前的结果集中,有多少个字段
  215. *
  216. * @param Resouce $query
  217. * @return int fields nums
  218. */
  219. public function numFields($query) {
  220. return @mysql_num_fields($query);
  221. }
  222.  
  223. /**
  224. * 获取当前执行的sql总条数
  225. *
  226. * @return queryNum
  227. */
  228. public function getQueryNum() {
  229. return $this->queryNum;
  230. }
  231.  
  232. /**
  233. * 获取当前文件中的函数,传入一个当前类存在函数,单例调用
  234. *
  235. * @param unknown_type $funcname
  236. * @param unknown_type $params
  237. * @return unknown
  238. */
  239. public function getFunc($funcname, $params = '') {
  240. if (empty($params)) {
  241. return $this->$funcname();
  242. } else {
  243. return $this->$funcname($this->getFuncParams($params));
  244. }
  245. }
  246.  
  247. /**
  248. * 如果是一个数组,那么拼接起来,处理返回一个参数集合
  249. *
  250. * @param array,string $params
  251. * @return string a,d,3
  252. */
  253. public function getFuncParams($params) {
  254. $returnStr = "";
  255. if (is_array($params)) {
  256. foreach ($params as $key => $val) {
  257. $returnStr .= $val . ",";
  258. }
  259. return rtrim($returnStr, ",");
  260. } else {
  261. return $params;
  262. }
  263. }
  264.  
  265. /**
  266. * 获取当前数据库的版本信息
  267. *
  268. * @return version
  269. */
  270. public function version() {
  271. return mysql_get_server_info($this->dbConn);
  272. }
  273.  
  274.  
  275. /**
  276. * 获取当前mysql数据的报错号
  277. * @return [type] [description]
  278. */
  279. public function errno() {
  280. return intval(@mysql_errno($this->dbConn));
  281. }
  282.  
  283. /**
  284. * 获取当前数据库的 提示信息
  285. * @return [type] [description]
  286. */
  287. public function error() {
  288. return @mysql_error($this->dbConn);
  289. }
  290.  
  291. /**
  292. * 操作数据库出错的提示信息
  293. * @param string $message [description]
  294. * @param string $sql [description]
  295. * @return [type] [description]
  296. */
  297. function halt($message = '', $sql = '') {
  298. $this->errormsg = "<b>MySQL Query : </b>$sql <br /><b> MySQL Error : </b>" . $this->error() . " <br /> <b>MySQL Errno : </b>" . $this->errno() . " <br /><b> Message : </b> $message";
  299. exit($this->errormsg);
  300. return false;
  301. }
  302.  
  303. /**
  304. * 展示所有表
  305. * @return [type] [description]
  306. */
  307. function showTable() {
  308. $tables = array();
  309. $result = $this->query("SHOW TABLES");
  310. while ($list = $this->fetchArray($result)) {
  311. $tables[] = $list['Tables_in_' . $this->dbName];
  312. }
  313. $this->freeResult($result);
  314. return $tables;
  315. }
  316.  
  317. }
  318. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement