首页 > 开发 > PHP > 正文

php实现比较全的数据库操作类

2024-05-04 23:36:30
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php实现比较全的数据库操作类,可实现基本的数据库连接、执行SQL语句及错误提示等相关技巧,需要的朋友可以参考下

本文实例讲述了php实现比较全的数据库操作类。分享给大家供大家参考。具体如下:

 

 
  1. <?php 
  2. class database 
  3. { 
  4. private $hostname; 
  5. private $user; 
  6. private $pass; 
  7. private $dbname; 
  8. private $linkflag; 
  9. private $charset; 
  10. function __construct() 
  11. { 
  12. $this->hostname="localhost"; 
  13. $this->user="root"; 
  14. $this->pass="111"; 
  15. $this->dbname=""; 
  16. $this->charset="utf8"; //gb2312 GBK utf8 
  17. $this->linkflag=mysql_connect($this->hostname,$this->user,$this->pass); 
  18. mysql_select_db($this->dbname,$this->linkflag) or die($this->error()); 
  19. mysql_query("set names ".$this->charset); 
  20. } 
  21. function __set($property_name,$value) 
  22. { 
  23. return $this->$property_name=$value; 
  24. } 
  25. function __get($property_name) 
  26. { 
  27. if(isset($this->$property_name)) 
  28. { 
  29. return $this->$property_name; 
  30. } 
  31. else return null; 
  32. } 
  33. function __call($function_name, $args) 
  34. { 
  35. echo "<br><font color=#ff0000>你所调用的方法 $function_name 不存在</font><br>/n"; 
  36. } 
  37. function query($sql) 
  38. { 
  39. $res=mysql_query($sql) or die($this->error()); 
  40. return $res; 
  41. } 
  42. function fetch_array($res) 
  43. { 
  44. return mysql_fetch_array($res); 
  45. } 
  46. function fetch_object($res) 
  47. { 
  48. return mysql_fetch_object($res); 
  49. } 
  50. function fetch_obj_arr($sql) 
  51. { 
  52. $obj_arr=array(); 
  53. $res=$this->query($sql); 
  54. while($row=mysql_fetch_object($res)) 
  55. { 
  56. $obj_arr[]=$row; 
  57. } 
  58. return $obj_arr; 
  59. } 
  60. function error() 
  61. { 
  62. if($this->linkflag) 
  63. { 
  64. return mysql_error($this->linkflag); 
  65. } 
  66. else return mysql_error(); 
  67. } 
  68. function errno() 
  69. { 
  70. if($this->linkflag) 
  71. { 
  72. return mysql_errno($this->linkflag); 
  73. } 
  74. else return mysql_errno(); 
  75. } 
  76. function affected_rows() 
  77. { 
  78. return mysql_affected_rows($this->linkflag); 
  79. } 
  80. function num_rows($sql) 
  81. { 
  82. $res=$this->execute($sql); 
  83. return mysql_num_rows($res); 
  84. } 
  85. function num_fields($res) 
  86. { 
  87. return mysql_num_fields($res); 
  88. } 
  89. function insert_id() 
  90. { 
  91. $previous_id=mysql_insert_id($this->linkflag); 
  92. return $previous_id; 
  93. } 
  94. function result($res,$row,$field=null) 
  95. { 
  96. if($field===null) 
  97. { 
  98. $res=mysql_result($res,$row); 
  99. } 
  100. else $res=mysql_result($res,$row,$field); 
  101. return $res; 
  102. } 
  103. function version() 
  104. { 
  105. return mysql_get_server_info($this->linkflag); 
  106. } 
  107. function data_seek($res,$rowNum) 
  108. { 
  109. return mysql_data_seek($res,$rowNum); 
  110. } 
  111. function __destruct() 
  112. { 
  113. //mysql_close($this->linkflag); 
  114. } 
  115. } 
  116. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表