首页  >  文章  >  后端开发  >  一个数据库操作PHP类

一个数据库操作PHP类

WBOY
WBOY原创
2016-07-25 08:44:271023浏览
  1. /*
  2. * Author 墨龙
  3. * Time 2010年12月2日 15:50:35
  4. */
  5. $db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);
  6. class mysql{
  7. private $db_host;
  8. private $db_user;
  9. private $db_password;
  10. private $db_table;
  11. private $db_conn; //数据库连接标识;
  12. private $result; //执行query命令的结果资源标识
  13. private $sql; //sql执行语句
  14. private $pre; //数据库表前缀
  15. private $coding; //数据库编码,GBK,UTF8,gb2312
  16. function __construct($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
  17. $this->db_host = $db_host;
  18. $this->db_user = $db_user;
  19. $this->db_password = $db_password;
  20. $this->db_table = $db_table;
  21. $this->db_conn = $db_conn;
  22. $this->pre = $pre;
  23. $this->coding = $coding;
  24. $this->connect();
  25. }
  26. function connect(){
  27. $this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("数据库链接错误,请检查数据库链接配置!"));
  28. if(!mysql_select_db($this->db_table,$this->db_conn)){
  29. echo "没有找到数据表:".$this->db_table;
  30. }
  31. mysql_select_db($this->db_table,$this->db_conn);
  32. $this->query("SET NAMES $this->coding");
  33. }
  34. /*执行SQL语句的函数*/
  35. function query($sql){
  36. if(emptyempty($sql)){
  37. $this->show_error("你的sql语句不能为空!");
  38. }else{
  39. $this->sql = $sql;
  40. }
  41. $result = mysql_query($this->sql,$this->db_conn);
  42. return $this->result = $result;
  43. }
  44. /*创建添加新的数据库*/
  45. public function create_database($database_name){
  46. $database=$database_name;
  47. $sqlDatabase = 'create database '.$database;
  48. return $this->query($sqlDatabase);
  49. }
  50. // 根据select查询结果计算结果集条数
  51. public function db_num_rows(){
  52. if($this->result==null){
  53. if($this->show_error){
  54. $this->show_error("sql语句错误!");
  55. }
  56. }else{
  57. return mysql_num_rows($this->result);
  58. }
  59. }
  60. /*查询服务器所有数据库*/
  61. //将系统数据库与用户数据库分开,更直观的显示?
  62. public function show_databases(){
  63. $this->query("show databases");
  64. echo "现有数据库:".$amount =$this->db_num_rows($rs);
  65. echo "
    ";
  66. $i=1;
  67. while($row = $this->fetch_array($rs)){
  68. echo "$i $row[Database]";
  69. echo "
    ";
  70. $i++;
  71. }
  72. }
  73. //以数组形式返回主机中所有数据库名
  74. public function databases()
  75. {
  76. $rsPtr=mysql_list_dbs($this->db_conn);
  77. $i=0;
  78. $cnt=mysql_num_rows($rsPtr);
  79. while($i {
  80. $rs[]=mysql_db_name($rsPtr,$i);
  81. $i++;
  82. }
  83. return print_r($rs);
  84. }
  85. /*查询数据库下所有的表*/
  86. function show_tables($database_name){
  87. $this->query("show tables");
  88. echo "现有数据库:".$amount = $this->db_num_rows($rs);
  89. echo "
    ";
  90. $i=1;
  91. while($row = $this->fetch_array($rs)){
  92. $columnName="Tables_in_".$database_name;
  93. echo "$i $row[$columnName]";
  94. echo "
    ";
  95. $i++;
  96. }
  97. }
  98. /*
  99. mysql_fetch_row() array $row[0],$row[1],$row[2]
  100. mysql_fetch_array() array $row[0] 或 $row[id]
  101. mysql_fetch_assoc() array 用$row->content 字段大小写敏感
  102. mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
  103. */
  104. /*取得记录集,获取数组-索引和关联,使用$row['content'] */
  105. public function fetch_array()
  106. {
  107. return @mysql_fetch_array($this->result);
  108. }
  109. //获取关联数组,使用$row['字段名']
  110. public function fetch_ass()
  111. {
  112. return @mysql_fetch_assoc($this->result);
  113. }
  114. //获取数字索引数组,使用$row[0],$row[1],$row[2]
  115. public function fetch_row()
  116. {
  117. return @mysql_fetch_row($this->result);
  118. }
  119. //获取对象数组,使用$row->content
  120. public function fetch_Object()
  121. {
  122. return @mysql_fetch_object($this->result);
  123. }
  124. //简化查询select
  125. public function findall($table){
  126. $table = $this->fulltablename($table);
  127. $this->query("select * from $table");
  128. }
  129. public function select($table,$columnName,$condition){
  130. $table = $this->fulltablename($table);
  131. if(emptyempty($columnName)){
  132. $columnName = "*";
  133. }
  134. $this->query("SELECT $columnName FROM $table $condition");
  135. }
  136. //简化的insert
  137. function insert($table,$arr){
  138. $table = $this->fulltablename($table);
  139. $sql = "INSERT INTO $table ";
  140. if(!is_array($arr)){
  141. $this->show_error("请输入参数数组!");
  142. }else{
  143. $k = "";
  144. $v = "";
  145. foreach($arr as $key => $value){
  146. $k .= "`$key`,";
  147. $v .= "'".$value."',";
  148. }
  149. }
  150. $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
  151. $this->query($sql);
  152. }
  153. //简化的update
  154. function update($table,$arr,$where){
  155. $table = $this->fulltablename($table);
  156. $sql = "UPDATE $table SET ";
  157. if(!is_array($arr)){
  158. $this->show_error("请输入参数数组!");
  159. }else{
  160. foreach($arr as $key => $value){
  161. $sql .= " `".$key."` = '".$value."' ,";
  162. }
  163. }
  164. $sql = substr($sql,0,-1)." where ".$where;
  165. return $this->query($sql);
  166. }
  167. //简化的delete
  168. function delete($table,$where = ""){
  169. $table = $this->fulltablename($table);
  170. if(emptyempty($where)){
  171. $this->show_error("条件不能为空!");
  172. }else{
  173. $where = " where ".$where;
  174. }
  175. $sql = "DELETE FROM $table ".$where;
  176. //echo $sql;
  177. return $this->query($sql);
  178. }
  179. //取得上一步 INSERT 操作产生的 ID
  180. public function insert_id(){
  181. return mysql_insert_id();
  182. }
  183. //加上前缀的数据表
  184. public function fulltablename($table){
  185. return $table = $this->pre.$table;
  186. }
  187. //查询字段数量
  188. public function num_fields($table){
  189. $table = $this->fulltablename($table);
  190. $this->query("select * from $table");
  191. echo "
    ";
  192. echo "字段数:".$total = mysql_num_fields($this->result);
  193. echo "
    ";  
  194. for ($i=0; $i print_r(mysql_fetch_field($this->result,$i) );
  195. }
  196. echo "";
  197. echo "
    ";
  198. }
  199. //取得 MySQL 服务器信息
  200. public function mysql_server($num=''){
  201. switch ($num){
  202. case 1 :
  203. return mysql_get_server_info(); //MySQL 服务器信息
  204. break;
  205. case 2 :
  206. return mysql_get_host_info(); //取得 MySQL 主机信息
  207. break;
  208. case 3 :
  209. return mysql_get_client_info(); //取得 MySQL 客户端信息
  210. break;
  211. case 4 :
  212. return mysql_get_proto_info(); //取得 MySQL 协议信息
  213. break;
  214. default:
  215. return mysql_get_client_info(); //默认取得mysql版本信息
  216. }
  217. }
  218. //析构函数,自动关闭数据库,垃圾回收机制
  219. /*public function __destruct()
  220. {
  221. if(!empty($this->result)){
  222. $this->free();
  223. }
  224. mysql_close($this->$db_conn);
  225. }*/
  226. /*获得客户端真实的IP地址*/
  227. function getip(){
  228. if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  229. {
  230. $ip = getenv("HTTP_CLIENT_IP");
  231. }
  232. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
  233. $ip = getenv("HTTP_X_FORWARDED_FOR");
  234. }
  235. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  236. {
  237. $ip = getenv("REMOTE_ADDR");
  238. }
  239. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
  240. $ip = $_SERVER['REMOTE_ADDR'];
  241. }
  242. else{
  243. $ip = "unknown";
  244. }
  245. return($ip);
  246. }
  247. function show_error($str){
  248. echo "";
  249. }
  250. }
  251. ?>
复制代码

PHP


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn