• 深入理解php的MySQL连接类


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

    参考自文章:

    php mysql连接类

    php与mysql连接类

    php测试mysql连接正常与否的代码

    代码:

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