php 操作MySql数据库类的简单示例

前端之家收集整理的这篇文章主要介绍了php 操作MySql数据库类的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. <?PHP
  2. /**
  3. * 操作MysqL数据库
  4. *
  5. * @param
  6. * @arrange (512.笔记) jb51.cc
  7. **/
  8. class DB{
  9. var $host;
  10. var $user_name;
  11. var $password;
  12. var $db_name;
  13. var $link_id;
  14. var $result;
  15. var $col;
  16. var $query;
  17. var $fields;
  18. var $records;
  19. var $setting;
  20. var $debug = false;
  21. var $query_count = 0;
  22. var $debug_file = "debug.sql";
  23. function settings($key,$value){
  24. $this->setting[$key] = $value;
  25. }
  26. function init($_host,$_user,$_password,$_db_name){
  27. $this->host = $_host;
  28. $this->user_name = $_user;
  29. $this->password = $_password;
  30. $this->db_name = $_db_name;
  31. $this->fields = array();
  32. $this->link_id = @MysqL_connect($_host,$_password) or die("Your website is not properly installed.");
  33. @MysqL_select_db($_db_name,$this->link_id);
  34. }
  35. function assign($field,$value){
  36. $this->fields[$field] = ($value)==""?("'".$value."'"):$value;
  37. }
  38. function assign_str($field,$value){
  39. $this->fields[$field] = "'".addslashes($value)."'";
  40. //$this->fields[$field] = "'".($value)."'";
  41. }
  42. function reset(){
  43. $this->fields = array();
  44. }
  45. function insert($table){
  46. $f = "";
  47. $v = "";
  48. reset($this->fields);
  49. foreach($this->fields as $field=>$value){
  50. $f.= ($f!=""?",":"").$field;
  51. $v.= ($v!=""?",":"").$value;
  52. }
  53. $sql = "INSERT INTO ".$table." (".$f.") VALUES (".$v.")";
  54. $this->query($sql);
  55. return $this->insert_id();
  56. }
  57. function update($table,$where){
  58. $f = "";
  59. reset($this->fields);
  60. foreach($this->fields as $field=>$value){
  61. $f.= ($f!=""?",":"").$field." = ".$value;
  62. }
  63. $sql = "UPDATE ".$table." SET ".$f." ".$where;
  64. $this->query($sql);
  65. }
  66. function timestampFormat($unixNumber){
  67. return date('Y-m-d H:i:s',$unixNumber);
  68. /// xxxx-xx-xx xx-xx-xx
  69. }
  70. function query($_query){
  71. list($usec,$sec) = explode(" ",microtime());
  72. $time_start = ((float)$usec + (float)$sec);
  73. $this->query = $_query;
  74. $this->result = @MysqL_query($_query,$this->link_id) or die( $_query."<p>".MysqL_error($this->link_id) );
  75. list($usec,microtime());
  76. $time_end = ((float)$usec + (float)$sec);
  77. $time = $time_end - $time_start;
  78. if($this->debug){
  79. $this->query_count ++;
  80. $f = fopen($this->debug_file,"a");
  81. $sss = "# ".$this->query_count."\n ".$time." sec \n\n".$_query."\n#-------------------------------------------------------------------------\n\n";
  82. fputs($f,$sss,strlen($sss));
  83. fclose($f);
  84. }
  85. return $this->result;
  86. }
  87. function get_records(){
  88. $this->records = array();
  89. while($row = @MysqL_fetch_array($this->result,MysqL_BOTH)){
  90. $this->records[count($this->records)] = $row;
  91. }
  92. reset($this->records);
  93. return $this->records;
  94. }
  95. function get_tables_status(){
  96. $this->query("SHOW TABLE STATUS FROM `".$this->db_name."`");
  97. if($this->num_rows() > 0){
  98. $tables = array();
  99. while($this->movenext()){
  100. $tables[$this->col["Name"]] = $this->col;
  101. }
  102. return $tables;
  103. }
  104. return false;
  105. }
  106. function fetch_array(){
  107. $this->col = @MysqL_fetch_array($this->result,MysqL_BOTH);
  108. }
  109. function num_rows(){
  110. return (int)@MysqL_num_rows($this->result);
  111. }
  112. function fixSlashes(){
  113. if($this->col){
  114. foreach($this->col as $key => $value)
  115. $this->col[$key] = stripslashes($value);
  116. return $this->col;
  117. }
  118. }
  119. function movenext(){
  120. $this->col=@MysqL_fetch_array($this->result,MysqL_ASSOC);
  121. if($this->setting['fixSlashes'])
  122. return $this->fixSlashes();
  123. else
  124. return $this->col;
  125. }
  126. function done(){
  127. @MysqL_close($this->link_id);
  128. }
  129. function insert_id(){
  130. return @MysqL_insert_id($this->link_id);
  131. }
  132. function affected_rows(){
  133. return @MysqL_affected_rows($this->link_id);
  134. }
  135. }
  136. /*** 来自:编程之家 jb51.cc(jb51.cc) ***/
  137. ?>

猜你在找的PHP相关文章