PHP myql 数据库 连接类

后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜欢在类的方法里直接用输出语句,不过这也只是列举数据库和表名,构造函数的参数也可以给个默认值吧。

[php]  view plain copy
  1. <?php  
  2.   
  3. /* 
  4. * filename:mysql数据库连接类 
  5. */  
  6.   
  7. class mysql{  
  8.   
  9.     private $db_host;  //数据库主机  
  10.     private $db_user;  //数据库用户名  
  11.     private $db_pwd;   //数据库用户名密码  
  12.     private $db_database;    //数据库名  
  13.     private $conn;           //数据库连接标识;  
  14.     private $result;         //执行query命令的结果资源标识  
  15.     private $sql;     //sql执行语句  
  16.     private $row;     //返回的条目数  
  17.     private $coding;  //数据库编码,GBK,UTF8,gb2312  
  18.     private $bulletin = true;    //是否开启错误记录  
  19.     private $show_error = true;  //测试阶段,显示所有错误,具有安全隐患,默认关闭  
  20.     private $is_error = false;   //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的  
  21.   
  22.   
  23.     /*构造函数*/  
  24.     public function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){  
  25.         $this->db_host=$db_host;  
  26.         $this->db_user=$db_user;  
  27.         $this->db_pwd = $db_pwd;  
  28.         $this->db_database=$db_database;  
  29.         $this->conn=$conn;  
  30.         $this->coding=$coding;  
  31.         $this->connect();  
  32.     }  
  33.   
  34.     /*数据库连接*/  
  35.     public function connect()  
  36.     {  
  37.         if($this->conn=="pconn"){  
  38.             //永久链接  
  39.             $this->conn=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);  
  40.         }else{  
  41.             //即时链接  
  42.             $this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);  
  43.         }  
  44.   
  45.         if(!mysql_select_db($this->db_database,$this->conn)){  
  46.             if($this->show_error){  
  47.                 $this->show_error("数据库不可用:",$this->db_database);  
  48.             }  
  49.         }  
  50.         mysql_query("SET NAMES $this->coding");  
  51.     }  
  52.   
  53.     /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/  
  54.     public function query($sql)  
  55.     {  
  56.         if($sql == ""){  
  57.         $this->show_error("sql语句错误:","sql查询语句为空");}  
  58.         $this->sql = $sql;  
  59.   
  60.         $result = mysql_query($this->sql,$this->conn);  
  61.   
  62.         if(!$result){  
  63.             //调试中使用,sql语句出错时会自动打印出来  
  64.             if($this->show_error){  
  65.                 $this->show_error("错误sql语句:",$this->sql);  
  66.             }  
  67.         }else{  
  68.             $this->result = $result;  
  69.         }  
  70.         return $this->result;  
  71.     }  
  72.   
  73.   
  74.     /*创建添加新的数据库*/  
  75.     public function create_database($database_name){  
  76.         $database=$database_name;  
  77.         $sqlDatabase = 'create database '.$database;  
  78.         $this->query($sqlDatabase);  
  79.     }  
  80.   
  81.     /*查询服务器所有数据库*/  
  82.     //将系统数据库与用户数据库分开,更直观的显示?  
  83.     public function show_databases(){  
  84.         $rs=$this->query("show databases");  
  85.         echo "现有数据库:".$amount =$this->db_num_rows($rs);  
  86.         echo "<br />";  
  87.         $i=1;  
  88.         while($row = $this->fetch_array($rs)){  
  89.             echo "$i $row[Database]";  
  90.             echo "<br />";  
  91.             $i++;  
  92.         }  
  93.     }  
  94.   
  95.     //以数组形式返回主机中所有数据库名  
  96.     public function databases()  
  97.     {  
  98.         $rsPtr=mysql_list_dbs($this->conn);  
  99.         $i=0;  
  100.         $cnt=mysql_num_rows($rsPtr);  
  101.         while($i<$cnt)  
  102.         {  
  103.           $rs[]=mysql_db_name($rsPtr,$i);  
  104.           $i++;  
  105.         }  
  106.         return $rs;  
  107.     }  
  108.   
  109.   
  110.     /*查询数据库下所有的表*/  
  111.     function show_tables($database_name){  
  112.         $this->query("show tables");  
  113.         echo "现有数据库:".$amount = $this->db_num_rows($rs);  
  114.         echo "<br />";  
  115.         $i=1;  
  116.         while($row = $this->fetch_array($rs)){  
  117.             $columnName="Tables_in_".$database_name;  
  118.             echo "$i $row[$columnName]";  
  119.             echo "<br />";  
  120.             $i++;  
  121.         }  
  122.     }  
  123.   
  124.     /* 
  125.     mysql_fetch_row()    array  $row[0],$row[1],$row[2] 
  126.     mysql_fetch_array()  array  $row[0] 或 $row[id] 
  127.     mysql_fetch_assoc()  array  用$row->content 字段大小写敏感 
  128.     mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感 
  129.     */  
  130.   
  131.     /*取得结果数据*/  
  132.     public function mysql_result_li()  
  133.     {  
  134.         return mysql_result($str);  
  135.     }  
  136.   
  137.     /*取得记录集,获取数组-索引和关联,使用$row['content'] */  
  138.     public function fetch_array()  
  139.     {  
  140.         return mysql_fetch_array($this->result);  
  141.     }  
  142.   
  143.   
  144.     //获取关联数组,使用$row['字段名']  
  145.     public function fetch_assoc()  
  146.     {  
  147.         return mysql_fetch_assoc($this->result);  
  148.     }  
  149.   
  150.     //获取数字索引数组,使用$row[0],$row[1],$row[2]  
  151.     public function fetch_row()  
  152.     {  
  153.         return mysql_fetch_row($this->result);  
  154.     }  
  155.   
  156.     //获取对象数组,使用$row->content  
  157.     public function fetch_Object()  
  158.     {  
  159.         return mysql_fetch_object($this->result);  
  160.     }  
  161.   
  162.   
  163.   
  164.     //简化查询select  
  165.     public function findall($table)  
  166.     {  
  167.         $this->query("SELECT * FROM $table");  
  168.     }  
  169.   
  170.   
  171.     //简化查询select  
  172.     public function select($table,$columnName,$condition)  
  173.     {  
  174.         if($columnName==""){  
  175.             $columnName="*";  
  176.         }  
  177.   
  178.         $this->query("SELECT $columnName FROM $table $condition");  
  179.   
  180.     }  
  181.   
  182.   
  183.   
  184.   
  185.     //简化删除del  
  186.     public function delete($table,$condition){  
  187.         $this->query("DELETE FROM $table WHERE $condition");  
  188.     }  
  189.   
  190.     //简化插入insert  
  191.     public function insert($table,$columnName,$value){  
  192.         $this->query("INSERT INTO $table ($columnName) VALUES ($value)");  
  193.     }  
  194.   
  195.     //简化修改update  
  196.     public function update($table,$mod_content,$condition){  
  197.         $this->query("UPDATE $table SET $mod_content WHERE $condition");  
  198.     }  
  199.   
  200.   
  201.     /*取得上一步 INSERT 操作产生的 ID*/  
  202.     public function insert_id(){  
  203.         return mysql_insert_id();  
  204.     }  
  205.   
  206.   
  207.   
  208.     //指向确定的一条数据记录  
  209.     public function db_data_seek($id){  
  210.         if($id>0){  
  211.             $id=$id-1;  
  212.         }  
  213.         if(!@mysql_data_seek($this->result,$id)){  
  214.             $this->show_error("sql语句有误:""指定的数据为空");  
  215.         }  
  216.         return $this->result;  
  217.     }  
  218.   
  219.   
  220.     // 根据select查询结果计算结果集条数  
  221.     public function db_num_rows(){  
  222.          if($this->result==null){  
  223.             if($this->show_error){  
  224.                 $this->show_error("sql语句错误","暂时为空,没有任何内容!");  
  225.             }  
  226.          }else{  
  227.             return  mysql_num_rows($this->result);  
  228.          }  
  229.     }  
  230.   
  231.     // 根据insert,update,delete执行结果取得影响行数  
  232.     public function db_affected_rows(){  
  233.          return mysql_affected_rows();  
  234.     }  
  235.   
  236.   
  237.     //输出显示sql语句  
  238.     public function show_error($message="",$sql=""){  
  239.         if(!$sql){  
  240.             echo "<font color='red'>".$message."</font>";  
  241.             echo "<br />";  
  242.         }else{  
  243.             echo "<fieldset>";  
  244.             echo "<legend>错误信息提示:</legend><br />";  
  245.             echo "<div style="font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;" mce_style="font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;">";  
  246.             echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";  
  247.             echo "<font color='white'>错误号:12142</font>";  
  248.             echo "</div><br />";  
  249.             echo "错误原因:".mysql_error()."<br /><br />";  
  250.             echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";  
  251.             echo "<font color='white'>".$message."</font>";  
  252.             echo "</div>";  
  253.             echo "<font color='red'><pre>".$sql."</pre></font>";  
  254.                 $ip=$this->getip();  
  255.                 if($this->bulletin){  
  256.                     $time = date("Y-m-d H:i:s");  
  257.                     $message=$message."/r/n$this->sql"."/r/n客户IP:$ip"."/r/n时间 :$time"."/r/n/r/n";  
  258.   
  259.                     $server_date=date("Y-m-d");  
  260.                     $filename=$server_date.".txt";  
  261.                     $file_path="error/".$filename;  
  262.                     $error_content=$message;  
  263.                     //$error_content="错误的数据库,不可以链接";  
  264.                     $file = "error"//设置文件保存目录  
  265.   
  266.                     //建立文件夹  
  267.                     if(!file_exists($file)){  
  268.                         if(!mkdir($file,0777)){  
  269.                         //默认的 mode 是 0777,意味着最大可能的访问权  
  270.                             die("upload files directory does not exist and creation failed");  
  271.                         }  
  272.                     }  
  273.   
  274.                     //建立txt日期文件  
  275.                     if(!file_exists($file_path)){  
  276.   
  277.                         //echo "建立日期文件";  
  278.                         fopen($file_path,"w+");  
  279.   
  280.                         //首先要确定文件存在并且可写  
  281.                         if (is_writable($file_path))  
  282.                         {  
  283.                             //使用添加模式打开$filename,文件指针将会在文件的开头  
  284.                             if (!$handle = fopen($file_path'a'))  
  285.                             {  
  286.                                 echo "不能打开文件 $filename";  
  287.                                 exit;  
  288.                             }  
  289.   
  290.                                 //将$somecontent写入到我们打开的文件中。  
  291.                             if (!fwrite($handle$error_content))  
  292.                             {  
  293.                                 echo "不能写入到文件 $filename";  
  294.                                 exit;  
  295.                             }  
  296.   
  297.                             //echo "文件 $filename 写入成功";  
  298.   
  299.                             echo "——错误记录被保存!";  
  300.   
  301.   
  302.                             //关闭文件  
  303.                             fclose($handle);  
  304.                         } else {  
  305.                             echo "文件 $filename 不可写";  
  306.                         }  
  307.   
  308.                     }else{  
  309.                         //首先要确定文件存在并且可写  
  310.                         if (is_writable($file_path))  
  311.                         {  
  312.                             //使用添加模式打开$filename,文件指针将会在文件的开头  
  313.                             if (!$handle = fopen($file_path'a'))  
  314.                             {  
  315.                                 echo "不能打开文件 $filename";  
  316.                                 exit;  
  317.                             }  
  318.   
  319.                                 //将$somecontent写入到我们打开的文件中。  
  320.                             if (!fwrite($handle$error_content))  
  321.                             {  
  322.                                 echo "不能写入到文件 $filename";  
  323.                                 exit;  
  324.                             }  
  325.   
  326.                             //echo "文件 $filename 写入成功";  
  327.                             echo "——错误记录被保存!";  
  328.   
  329.                             //关闭文件  
  330.                             fclose($handle);  
  331.                         } else {  
  332.                             echo "文件 $filename 不可写";  
  333.                         }  
  334.                     }  
  335.   
  336.                 }  
  337.                 echo "<br />";  
  338.                 if($this->is_error){  
  339.                     exit;  
  340.                 }  
  341.             }  
  342.             echo "</div>";  
  343.             echo "</fieldset>";  
  344.   
  345.   
  346.   
  347.         echo "<br />";  
  348.     }  
  349.   
  350.   
  351.     //释放结果集  
  352.     public function free(){  
  353.         @mysql_free_result($this->result);  
  354.     }  
  355.   
  356.     //数据库选择  
  357.     public function select_db($db_database){  
  358.         return mysql_select_db($db_database);  
  359.     }  
  360.   
  361.     //查询字段数量  
  362.     public function num_fields($table_name){  
  363.         //return mysql_num_fields($this->result);  
  364.         $this->query("select * from $table_name");  
  365.         echo "<br />";  
  366.         echo "字段数:".$total = mysql_num_fields($this->result);  
  367.         echo "<pre>";  
  368.         for ($i=0; $i<$total$i++){  
  369.             print_r(mysql_fetch_field($this->result,$i) );  
  370.         }  
  371.         echo "</pre>";  
  372.         echo "<br />";  
  373.     }  
  374.   
  375.     //取得 MySQL 服务器信息  
  376.     public function mysql_server($num=''){  
  377.         switch ($num){  
  378.             case 1 :  
  379.             return mysql_get_server_info(); //MySQL 服务器信息  
  380.             break;  
  381.   
  382.             case 2 :  
  383.             return mysql_get_host_info();   //取得 MySQL 主机信息  
  384.             break;  
  385.   
  386.             case 3 :  
  387.             return mysql_get_client_info(); //取得 MySQL 客户端信息  
  388.             break;  
  389.   
  390.             case 4 :  
  391.             return mysql_get_proto_info();  //取得 MySQL 协议信息  
  392.             break;  
  393.   
  394.             default:  
  395.             return mysql_get_client_info(); //默认取得mysql版本信息  
  396.         }  
  397.     }  
  398.   
  399.     //析构函数,自动关闭数据库,垃圾回收机制  
  400.     public function __destruct()  
  401.     {  
  402.         if(!empty($this->result)){  
  403.             $this->free();  
  404.         }  
  405.         mysql_close($this->conn);  
  406.     }//function __destruct();  
  407.   
  408.   
  409.   
  410.     /*获得客户端真实的IP地址*/  
  411.     function getip(){  
  412.         if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))  
  413.         {  
  414.             $ip = getenv("HTTP_CLIENT_IP");  
  415.         }  
  416.         else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){  
  417.             $ip = getenv("HTTP_X_FORWARDED_FOR");  
  418.         }  
  419.         else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))  
  420.         {  
  421.             $ip = getenv("REMOTE_ADDR");  
  422.         }  
  423.         else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){  
  424.         $ip = $_SERVER['REMOTE_ADDR'];  
  425.         }  
  426.         else{  
  427.             $ip = "unknown";  
  428.         }  
  429.         return($ip);  
  430.     }  
  431. }  
  432. ?>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值