php简单操作mysql数据库的类

这篇文章主要介绍了php简单操作mysql数据库的类,涉及php操作mysql的连接、查询、插入、删除等基本操作方法,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php简单操作mysql数据库的类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * Database class
  4. *
  5. * @version: 2.2
  6. * @revised: 27 may 2007
  7. *
  8. **/
  9. class Database {
  10. var $host;
  11. var $name;
  12. var $user;
  13. var $pass;
  14. var $prefix;
  15. var $linkId;
  16. function Database($mysql) {
  17. foreach($mysql as $k => $v){
  18. $this->$k = $v;
  19. }
  20. if(strlen($this->prefix)>0 && substr($this->prefix, -1) !== "_")
  21. $prefix .= "_";
  22. $this->prefix = $prefix;
  23. }
  24. function getLastID() {
  25. $id = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()", $this->linkId));
  26. return $id[0];
  27. }
  28. function getPossibleValues($tableA, $whereA) {
  29. if(gettype($tableA) == "array") {
  30. $table = "";
  31. foreach($tableA as $t) {
  32. $table .= $this->prefix.$t.", ";
  33. }
  34. $table = substr($table, 0, -2);
  35. } else $table = $this->prefix.$tableA;
  36. if(gettype($whereA) != "array") {
  37. $whereA = array($whereA);
  38. }
  39. $return = array();
  40. foreach($whereA as $where) {
  41. $sql = mysql_query("SHOW COLUMNS FROM ".$table." LIKE '%".$where."%'");
  42. while($arr = mysql_fetch_array($sql)) {
  43. if(strpos($arr['Type'], 'enum')===0) {
  44. $vals = substr($arr['Type'], 5, -1);
  45. } else {
  46. $vals = substr($arr['Type'], 4, -1);
  47. }
  48. $vals = str_replace("'","",$vals);
  49. $vals = explode(",",$vals);
  50. $i = 1;
  51. foreach($vals as $val) {
  52. $return[$arr['Field']][$i++] = $val;
  53. }
  54. $return[$arr['Field']]['default'] = $arr['Default'];
  55. if($arr['Null'] != "NO") $return[$arr['Field']][0] = NULL;
  56. }
  57. }
  58. return $return;
  59. }
  60. function connect() {
  61. $this->linkId = mysql_connect($this->host, $this->user, $this->pass);
  62. if(!$this->linkId) {
  63. return false;
  64. }
  65. if(mysql_select_db($this->name, $this->linkId)) return true;
  66. mysql_close($this->linkId);
  67. return false;
  68. }
  69. function runSelect($tables, $where = "1", $fieldsA = "*", $order = false, $limit = false, $offset = false, $group = false) {
  70. if(gettype($tables) == "array") {
  71. $table = "";
  72. foreach($tables as $t) {
  73. $table .= $this->prefix.$t.", ";
  74. }
  75. $table = substr($table, 0, -2);
  76. } else $table = $this->prefix.$tables;
  77. if(gettype($fieldsA) == "array") {
  78. $fields = "";
  79. $keys = array_keys($fieldsA);
  80. if($keys[0] != '0') {
  81. foreach($keys as $key) {
  82. $fields .= $key.' AS '.$fieldsA[$key].', ';
  83. }
  84. } else {
  85. foreach($fieldsA as $field) {
  86. $fields .= $field.', ';
  87. }
  88. }
  89. $fields = substr($fields, 0, -2);
  90. } else $fields = $fieldsA;
  91. $query = "SELECT ".$fields." FROM ".$table." WHERE ".$where.
  92. ($order!== false?" ORDER BY ".$order:($group!==false ? " GROUP BY ".$group : "")).
  93. ($limit !== false?" LIMIT ".$limit:"").
  94. ($offset !== false?" OFFSET ".$offset:"");
  95. return mysql_query($query, $this->linkId);
  96. }
  97. function runUpdate($table, $valuesA, $where = "1") {
  98. if(gettype($valuesA) == "array") {
  99. $fields = "";
  100. $values = "";
  101. $keys = array_keys($valuesA);
  102. foreach($keys as $key) {
  103. if($valuesA[$key] !== NULL)
  104. $values .= "`".$key."`='".str_replace("'",'\'', $valuesA[$key])."',";
  105. else
  106. $values .= $key."=NULL,";
  107. }
  108. $fields = substr($fields, 0, -1);
  109. $values = substr($values, 0, -1);
  110. } else $values = $valuesA;
  111. $query = "UPDATE ".$this->prefix.$table." SET ".$values." WHERE ".$where;
  112. if(mysql_query($query,
  113. $this->linkId))
  114. return mysql_affected_rows($this->linkId);
  115. return false;
  116. }
  117. function runDelete($table, $where = "1") {
  118. if(mysql_query("DELETE FROM ".$this->prefix.$table." WHERE ".$where, $this->linkId))
  119. return mysql_affected_rows($this->linkId);
  120. return false;
  121. }
  122. function runInsert($table, $valuesA, $onDuplicate = NULL) {
  123. if(gettype($valuesA) == "array") {
  124. $fields = "";
  125. $values = "";
  126. $keys = array_keys($valuesA);
  127. foreach($keys as $key) {
  128. $fields .= "`".$key."`, ";
  129. $values .= ($valuesA[$key]===NULL?"NULL, ":"'".str_replace("'", '\'', $valuesA[$key])."', ");
  130. }
  131. $fields = substr($fields, 0, -2);
  132. $values = substr($values, 0, -2);
  133. }
  134. $onDup = "";
  135. if($onDuplicate != NULL) {
  136. $onDup = " ON DUPLICATE KEY UPDATE ";
  137. if(gettype($onDuplicate) == "array") {
  138. $keys = array_keys($onDuplicate);
  139. foreach($keys as $key) {
  140. $onDup .= '`'.$key.'`='.($onDuplicate[$key]===NULL?"NULL,":"'".str_replace("'", '\'', $onDuplicate[$key])."', ");
  141. }
  142. $onDup = substr($onDup, 0, -2);
  143. } else $onDup .= $onDuplicate;
  144. }
  145. $query = "INSERT INTO ".$this->prefix.$table.($fields!==NULL?"(".$fields.")":"").
  146. " VALUES (".$values.")".$onDup;
  147. if(mysql_query($query, $this->linkId))
  148. return mysql_affected_rows($this->linkId);
  149. return false;
  150. }
  151. function getCells($table){
  152. $query = "SHOW COLUMNS FROM `".$table."`";
  153. $fields = mysql_query($query, $this->linkId) or die('hej');
  154. return $fields;
  155. }
  156. function translateCellName($cellName){
  157. $sql = $this->runSelect("mysql_cell_translation","mysql_name = '".$cellName."'");
  158. $row = mysql_fetch_assoc($sql);
  159. return $row['human_name']?$row['human_name']:'<span class="faded">['.$cellName.']</span>';
  160. }
  161. function getError() {
  162. return mysql_error($this->linkId);
  163. }
  164. function close()
  165. {
  166. mysql_close($this->linkId);
  167. }
  168. }
  169. ?>