服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Mssql|Oracle|Redis|

服务器之家 - 数据库 - Mysql - Mysql 数据库访问类

Mysql 数据库访问类

2019-10-25 16:06mysql教程网 Mysql

Mysql数据库访问类 实现代码,对于想学习mysql操作类的朋友值得一看

  1. /**  
  2. * @Purpose: Mysql数据库访问类  
  3. * @Package:  
  4. * @Author: lisen@sellingclub.cn  
  5. * @Modifications:  
  6. * @See:  
  7. * @Time: 2008.10.10  
  8. */  
  9. class DB_MYSQL  
  10. {  
  11. //============================================================  
  12. private $Host = 'localhost';  
  13. private $Database = 'db_name';  
  14. private $User = 'user';  
  15. private $Password = 'password';  
  16. //============================================================  
  17. private $Link_Id = 0; //数据库连接  
  18. private $Query_Id = 0; //查询结果  
  19. private $Row_Result = array(); //结果集组成的数组  
  20. private $Field_Result = array(); //结果集字段组成的数组  
  21. private $Affected_Rows; //影响的行数  
  22. private $Rows; //结果集中记录的行数  
  23. private $Fields; //结果集中字段数  
  24. private $Row_Postion = 0; //记录指针位置索引  
  25. public $Insert_Id = 0;  
  26. //************************************************************  
  27. /**** 构造函数 ****/  
  28. function __construct()  
  29. {  
  30. $this->connect();  
  31. }  
  32. /**** 析构函数 ****/  
  33. function __destruct()  
  34. {  
  35. @mysql_free_result($this->Query_Id);  
  36. mysql_close($this->Link_Id);  
  37. }  
  38. /**** 连接服务器,选择数据库 ****/  
  39. function connect($Database = '',$Host = '',$User = '',$Password = '')  
  40. {  
  41. $Database = $Database == '' ? $this->Database : $Database;  
  42. $Host = $Host == '' ? $this->Host : $Host;  
  43. $User = $User == '' ? $this->User : $User;  
  44. $Password = $Password == '' ? $this->Password : $Password;  
  45. //-----------------------------------------------------------//  
  46. if(0 == $this->Link_Id)  
  47. {  
  48. $this->Link_Id = @mysql_pconnect($Host,$User,$Password);  
  49. if(!$this->Link_Id)  
  50. {  
  51. $this->halt('连接数据库服务端失败!');  
  52. }  
  53. if(!mysql_select_db($this->Database,$this->Link_Id))  
  54. {  
  55. $this->halt('不能打开指定的数据库:'.$this->Database);  
  56. }  
  57. }  
  58. return $this->Link_Id;  
  59. }  
  60. /**** 释放内存 ****/  
  61. function free()  
  62. {  
  63. if(@mysql_free_result($this->Query_Id))  
  64. {  
  65. unset($this->Row_Result);  
  66. }  
  67. $this->Query_Id = 0;  
  68. }  
  69. /**** 执行查询 ****/  
  70. function query($Query_String)  
  71. {  
  72. //释放上次查询内存  
  73. if($this->Query_Id)  
  74. {  
  75. $this->free();  
  76. }  
  77. if(0 == $this->Link_Id)  
  78. {  
  79. $this->connect();  
  80. }  
  81. //设置中文字符集  
  82. @mysql_query('set names gb2312');  
  83. $this->Query_Id = mysql_query($Query_String,$this->Link_Id);  
  84. $this->Insert_Id = mysql_insert_id();  
  85. if(!$this->Query_Id)  
  86. {  
  87. $this->halt('SQL查询语句出错:'.$Query_String);  
  88. }  
  89. @mysql_query('set names gb2312');  
  90. return $this->Query_Id;  
  91. }  
  92. /**** 将结果集指针指向指定行 ****/  
  93. function seek($pos)  
  94. {  
  95. if(@mysql_data_seek($this->Query_Id,$pos))  
  96. {  
  97. $this->Row_Position = $pos;  
  98. return true;  
  99. }  
  100. else  
  101. {  
  102. $this->halt('定位结果集发生错误!');  
  103. return false;  
  104. }  
  105. }  
  106. /**** 返回结果集组成的数组 ****/  
  107. function get_rows_array()  
  108. {  
  109. $this->get_rows();  
  110. for($i = 0; $i < $this->Rows; $i++)  
  111. {  
  112. if(!mysql_data_seek($this->Query_Id,$i))  
  113. {  
  114. $this->halt('mysql_data_seek 查询出错!');  
  115. }  
  116. $this->Row_Result[$i] = mysql_fetch_array($this->Query_Id);  
  117. }  
  118. return $this->Row_Result;  
  119. }  
  120. /**** 返回结果集字段组成的数组 ****/  
  121. function get_fields_array()  
  122. {  
  123. $this->get_fields();  
  124. for($i = 0; $i < $this->Fields; $i++)  
  125. {  
  126. $obj = mysql_fetch_field($this->Query_Id,$i);  
  127. $this->Field_Result[$i] = $obj->name;  
  128. }  
  129. return $this->Field_Result;  
  130. }  
  131. /**** 返回影响记录数 ****/  
  132. function get_affected_rows()  
  133. {  
  134. $this->Affected_Rows = mysql_affected_rows($this->Link_Id);  
  135. return $this->Affected_Rows;  
  136. }  
  137. /**** 返回结果集中的记录数 ****/  
  138. function get_rows()  
  139. {  
  140. $this->Rows = mysql_num_rows($this->Query_Id);  
  141. return $this->Rows;  
  142. }  
  143. /**** 返回结果集中的字段个数 ****/  
  144. function get_fields()  
  145. {  
  146. $this->Fields = mysql_num_fields($this->Query_Id);  
  147. return $this->Fields;  
  148. }  
  149. /**** 执行sql语句并返回由查询结果中第一行记录组成的数组 ****/  
  150. function fetch_one_array($sql)  
  151. { @mysql_query('set names gb2312');  
  152. $this->query($sql);  
  153. return mysql_fetch_array($this->Query_Id);  
  154. }  
  155. /**** 打印错误信息 ****/  
  156. function halt($msg)  
  157. {  
  158. $this->Error = mysql_error();  
  159. printf("<font style='font-family:Arial,宋体;font-size:12px;'> <b>数据库发生错误:</b> %s \n",$msg);  
  160. printf("MySQL 返回错误信息:</b> %s \n",$this->Error);  
  161. printf("错误页面:<font style='color:#0000EE;text-decoration:underline'>%s</font> \n",$_SERVER['PHP_SELF']);  
  162. printf(" 请将错误信息提交到系统管理员或网站程序员处理! \n");  
  163. die('<b><font color=red>脚本终止</font></b></font>');  
  164. }  

延伸 · 阅读

精彩推荐