PHP操作数据库的Class/类

下面一个自己开发应用中会常用到的一款数据库操作类代码,下面有两个文件一个是配置文件一个是操作数据库类,最后简单的列举了这个例类的调用方法.

配置文件config.db.php,代码如下:

  1. <?php
  2. $db_config["hostname"] = "localhost"; //服务器地址
  3. $db_config["username"] = "root"; //数据库用户名
  4. $db_config["password"] = "123"; //数据库密码
  5. $db_config["database"] = "test"; //数据库名称
  6. $db_config["charset"] = "utf8";//数据库编码
  7. $db_config["pconnect"] = 1;//开启持久连接
  8. $db_config["log"] = 1;//开启日志
  9. $db_config["logfilepath"] = './';//开启日志
  10. ?>

数据库操作类,代码如下:

  1. <!--?php
  2. Class DB {
  3. private $link_id;
  4. private $handle;
  5. private $is_log;
  6. private $time;
  7. //构造函数
  8. public function __construct() {
  9. $this--->time = $this->microtime_float();
  10. require_once("config.db.php");
  11. $this->connect($db_config["hostname"], $db_config["username"], $db_config["password"], $db_config["database"], $db_config["pconnect"]);
  12. $this->is_log = $db_config["log"];
  13. if($this->is_log){
  14. $handle = fopen($db_config["logfilepath"]."dblog.txt", "a+");
  15. $this->handle=$handle;
  16. }
  17. }
  18. //数据库连接
  19. public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8') {
  20. if( $pconnect==0 ) {
  21. $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
  22. if(!$this->link_id){
  23. $this->halt("数据库连接失败");
  24. }
  25. } else {
  26. $this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw);
  27. if(!$this->link_id){
  28. $this->halt("数据库持久连接失败");
  29. }
  30. }
  31. if(select_db($dbname,$this-%3Elink_id">!@mysql_select_db($dbname,$this->link_id)) {
  32. $this->halt('数据库选择失败');
  33. }
  34. @mysql_query("set names ".$charset);
  35. }
  36. //查询
  37. public function query($sql) {
  38. $this->write_log("查询 ".$sql);
  39. $query = mysql_query($sql,$this->link_id);
  40. if(!$query) $this->halt('Query Error: ' . $sql);
  41. return $query;
  42. }
  43. //获取一条记录(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH)
  44. public function get_one($sql,$result_type = MYSQL_ASSOC) {
  45. $query = $this->query($sql);
  46. $rt =& mysql_fetch_array($query,$result_type);
  47. $this->write_log("获取一条记录 ".$sql);
  48. return $rt;
  49. }
  50. //获取全部记录
  51. public function get_all($sql,$result_type = MYSQL_ASSOC) {
  52. $query = $this->query($sql);
  53. $i = 0;
  54. $rt = array();
  55. while($row =& mysql_fetch_array($query,$result_type)) {
  56. $rt[$i]=$row;
  57. $i++;
  58. }
  59. $this->write_log("获取全部记录 ".$sql);
  60. return $rt;
  61. }
  62. //插入
  63. public function insert($table,$dataArray) {
  64. $field = "";
  65. $value = "";
  66. if( !is_array($dataArray) || count($dataArray)<=0) {
  67. $this->halt('没有要插入的数据');
  68. return false;
  69. }
  70. while(list($key,$val)=each($dataArray)) {
  71. $field .="$key,";
  72. $value .="'$val',";
  73. }
  74. $field = substr( $field,0,-1);
  75. $value = substr( $value,0,-1);
  76. $sql = "insert into $table($field) values($value)";
  77. $this->write_log("插入 ".$sql);
  78. if(!$this->query($sql)) return false;
  79. return true;
  80. }
  81. //更新
  82. public function update( $table,$dataArray,$condition="") {
  83. if( !is_array($dataArray) || count($dataArray)<=0) {
  84. $this->halt('没有要更新的数据');
  85. return false;
  86. }
  87. $value = "";
  88. while( list($key,$val) = each($dataArray))
  89. $value .= "$key = '$val',";
  90. $value .= substr( $value,0,-1);
  91. $sql = "update $table set $value where 1=1 and $condition";
  92. $this->write_log("更新 ".$sql);
  93. if(!$this->query($sql)) return false;
  94. return true;
  95. }
  96. //删除
  97. public function delete( $table,$condition="") {
  98. if( emptyempty($condition) ) {
  99. $this->halt('没有设置删除的条件');
  100. return false;
  101. }
  102. $sql = "delete from $table where 1=1 and $condition";
  103. $this->write_log("删除 ".$sql);
  104. if(!$this->query($sql)) return false;
  105. return true;
  106. }
  107. //返回结果集
  108. public function fetch_array($query, $result_type = MYSQL_ASSOC){
  109. $this->write_log("返回结果集");
  110. return mysql_fetch_array($query, $result_type);
  111. }
  112. //获取记录条数
  113. public function num_rows($results) {
  114. if(!is_bool($results)) {
  115. $num = mysql_num_rows($results);
  116. $this->write_log("获取的记录条数为".$num);
  117. return $num;
  118. } else {
  119. return 0;
  120. }
  121. }
  122. //释放结果集
  123. public function free_result() {
  124. $void = func_get_args();
  125. foreach($void as $query) {
  126. if(is_resource($query) && get_resource_type($query) === 'mysql result') {
  127. return mysql_free_result($query);
  128. }
  129. }
  130. $this->write_log("释放结果集");
  131. }
  132. //获取最后插入的id
  133. public function insert_id() {
  134. $id = mysql_insert_id($this->link_id);
  135. $this->write_log("最后插入的id为".$id);
  136. return $id;
  137. }
  138. //关闭数据库连接
  139. protected function close() {
  140. $this->write_log("已关闭数据库连接");
  141. return @mysql_close($this->link_id);
  142. }
  143. //错误提示
  144. private function halt($msg='') {
  145. $msg .= "\r\n".mysql_error();
  146. $this->write_log($msg);
  147. die($msg);
  148. }
  149. //析构函数
  150. public function __destruct() {
  151. $this->free_result();
  152. $use_time = ($this-> microtime_float())-($this->time);
  153. $this->write_log("完成整个查询任务,所用时间为".$use_time);
  154. if($this->is_log){
  155. fclose($this->handle);
  156. }
  157. }
  158. //写入日志文件
  159. public function write_log($msg=''){
  160. if($this->is_log){
  161. $text = date("Y-m-d H:i:s")." ".$msg."\r\n";
  162. fwrite($this->handle,$text);
  163. } //开源软件:phpfensi.com
  164. }
  165. //获取毫秒数
  166. public function microtime_float() {
  167. list($usec, $sec) = explode(" ", microtime());
  168. return ((float)$usec + (float)$sec);
  169. }
  170. }
  171. ?>

使用方法,代码如下:

  1. $db = new DB();
  2. //调用
  3. $db->insert_id();//获取最新IP地址