php专区

 首页 > php专区 > PHP应用 > php类库 > mysql数据库连接操作类 - php类库

mysql数据库连接操作类 - php类库

分享到:
【字体:
导读:
         [导读] 分享一篇可以记录错误日志的mysql数据库连接类文件,实例代码如下:?phpclassmysql{private$db_host;//数据库主机private$db_user;//数据库......

mysql数据库连接操作类

分享一篇可以记录错误日志的mysql数据库连接类文件,实例代码如下:

  1. class mysql { 
  2.  private $db_host//数据库主机 
  3.  private $db_user//数据库用户名 
  4.  private $db_pwd//数据库用户名密码 
  5.  private $db_database//数据库名 
  6.  private $conn//数据库连接标识; 
  7.  private $result//执行query命令的结果资源标识 
  8.  private $sql//sql执行语句 
  9.  private $row//返回的条目数 
  10.  private $coding//数据库编码,GBK,UTF8,gb2312 
  11.  private $bulletin = true; //是否开启错误记录 
  12.  private $show_error = true; //测试阶段,显示所有错误,具有安全隐患,默认关闭 
  13.  private $is_error = false; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的 
  14.  
  15.  /*构造函数*/ 
  16.  public function __construct($db_host$db_user$db_pwd$db_database$conn$coding) { 
  17.   $this->db_host = $db_host
  18.   $this->db_user = $db_user
  19.   $this->db_pwd = $db_pwd
  20.   $this->db_database = $db_database
  21.   $this->conn = $conn
  22.   $this->coding = $coding
  23.   $this->connect(); 
  24.  } 
  25.  
  26.  /*数据库连接*/ 
  27.  public function connect() { 
  28.   if ($this->conn == "pconn") { 
  29.    //永久链接 
  30.    $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); 
  31.   } else { 
  32.    //即使链接 
  33.    $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); 
  34.   } 
  35.  
  36.   if (!mysql_select_db($this->db_database, $this->conn)) { 
  37.    if ($this->show_error) { 
  38.     $this->show_error("数据库不可用:"$this->db_database); 
  39.    } 
  40.   } 
  41.   mysql_query("SET NAMES $this->coding"); 
  42.  } 
  43.  
  44.  /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/ 
  45.  public function query($sql) { 
  46.   if ($sql == "") { 
  47.    $this->show_error("SQL语句错误:""SQL查询语句为空"); 
  48.   } 
  49.   $this->sql = $sql
  50.  
  51.   $result = mysql_query($this->sql, $this->conn); 
  52.  
  53.   if (!$result) { 
  54.    //调试中使用,sql语句出错时会自动打印出来 
  55.    if ($this->show_error) { 
  56.     $this->show_error("错误SQL语句:"$this->sql); 
  57.    } 
  58.   } else { 
  59.    $this->result = $result
  60.   } 
  61.   return $this->result; 
  62.  } 
  63.  
  64.  /*创建添加新的数据库*/ 
  65.  public function create_database($database_name) { 
  66.   $database = $database_name
  67.   $sqlDatabase = 'create database ' . $database
  68.   $this->query($sqlDatabase); 
  69.  } 
  70.  
  71.  /*查询服务器所有数据库*/ 
  72.  //将系统数据库与用户数据库分开,更直观的显示? 
  73.  public function show_databases() { 
  74.   $this->query("show databases"); 
  75.   echo "现有数据库:" . $amount = $this->db_num_rows($rs); 
  76.   echo ""
  77.   $i = 1; 
  78.   while ($row = $this->fetch_array($rs)) { 
  79.    echo "$i $row[Database]"
  80.    echo ""
  81.    $i++; 
  82.   } 
  83.  } 
  84.  
  85.  //以数组形式返回主机中所有数据库名 
  86.  public function databases() { 
  87.   $rsPtr = mysql_list_dbs($this->conn); 
  88.   $i = 0; 
  89.   $cnt = mysql_num_rows($rsPtr); 
  90.   while ($i < $cnt) { 
  91.    $rs[] = mysql_db_name($rsPtr$i); 
  92.    $i++; 
  93.   } 
  94.   return $rs
  95.  } 
  96.  
  97.  /*查询数据库下所有的表*/ 
  98.  public function show_tables($database_name) { 
  99.   $this->query("show tables"); 
  100.   echo "现有数据库:" . $amount = $this->db_num_rows($rs); 
  101.   echo ""
  102.   $i = 1; 
  103.   while ($row = $this->fetch_array($rs)) { 
  104.    $columnName = "Tables_in_" . $database_name
  105.    echo "$i $row[$columnName]"
  106.    echo ""
  107.    $i++; 
  108.   } 
  109.  } 
  110.  
  111.  /* 
  112.  mysql_fetch_row()    array  $row[0],$row[1],$row[2] 
  113.  mysql_fetch_array()  array  $row[0] 或 $row[id] 
  114.  mysql_fetch_assoc()  array  用$row->content 字段大小写敏感 
  115.  mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感 
  116.  */ 
  117.  
  118.  /*取得结果数据*/ 
  119.  public function mysql_result_li() { 
  120.   return mysql_result($str); 
  121.  } 
  122.  
  123.  /*取得记录集,获取数组-索引和关联,使用$row['content'] */ 
  124.  
  125.  public function fetch_array() { 
  126.   return mysql_fetch_array($this->result); 
  127.  } 
  128.  
  129. // public function fetch_array($query) { 
  130. //  return mysql_fetch_array($query); 
  131. // } 
  132.  
  133.  //获取关联数组,使用$row['字段名'] 
  134.  public function fetch_assoc() { 
  135.   return mysql_fetch_assoc($this->result); 
  136.  } 
  137.  
  138.  //获取数字索引数组,使用$row[0],$row[1],$row[2] 
  139.  public function fetch_row() { 
  140.   return mysql_fetch_row($this->result); 
  141.  } 
  142.  
  143.  //获取对象数组,使用$row->content 
  144.  public function fetch_Object() { 
  145.   return mysql_fetch_object($this->result); 
  146.  } 
  147.  
  148.  //简化查询select 
  149.  public function findall($table) { 
  150.   $this->query("SELECT * FROM $table"); 
  151.  } 
  152.  
  153.  //简化查询select 
  154.  public function select($table$columnName = "*"$condition = ''$debug = '') { 
  155.   $condition = $condition ? ' Where ' . $condition : NULL; 
  156.   if ($debug) { 
  157.    echo "SELECT $columnName FROM $table $condition"
  158.   } else { 
  159.    $this->query("SELECT $columnName FROM $table $condition"); 
  160.   } 
  161.  } 
  162.  
  163.  //简化删除del 
  164.  public function delete($table$condition$url = '') { 
  165.   if ($this->query("DELETE FROM $table WHERE $condition")) { 
  166.    if (!emptyempty ($url)) 
  167.     $this->Get_admin_msg($url'删除成功!'); 
  168.   } 
  169.  } 
  170.  
  171.  //简化插入insert 
  172.  public function insert($table$columnName$value$url = '') { 
  173.   if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) { 
  174.    if (!emptyempty ($url)) 
  175.     $this->Get_admin_msg($url'添加成功!'); 
  176.   } 
  177.  } 
  178.  
  179.  //简化修改update 
  180.  public function update($table$mod_content$condition$url = '') { 
  181.   //echo "UPDATE $table SET $mod_content WHERE $condition"; exit(); 
  182.   if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) { 
  183.    if (!emptyempty ($url)) 
  184.     $this->Get_admin_msg($url); 
  185.   } 
  186.  } 
  187.  
  188.  /*取得上一步 INSERT 操作产生的 ID*/ 
  189.  public function insert_id() { 
  190.   return mysql_insert_id(); 
  191.  } 
  192.  
  193.  //指向确定的一条数据记录 
  194.  public function db_data_seek($id) { 
  195.   if ($id > 0) { 
  196.    $id = $id -1; 
  197.   } 
  198.   if (!@ mysql_data_seek($this->result, $id)) { 
  199.    $this->show_error("SQL语句有误:""指定的数据为空"); 
  200.   } 
  201.   return $this->result; 
  202.  } 
  203.  
  204.  // 根据select查询结果计算结果集条数 
  205. /* public function db_num_rows() { 
  206.   if ($this->result == null) { 
  207.    if ($this->show_error) { 
  208.     $this->show_error("SQL语句错误", "暂时为空,没有任何内容!"); 
  209.    } 
  210.   } else { 
  211.    return mysql_num_rows($this->result); 
  212.   } 
  213.  }*/ 
  214.  public function db_num_rows($result){ 
  215.   if($result==null){ 
  216.   if ($this->show_error) { 
  217.     $this->show_error("SQL语句错误""暂时为空,没有任何内容!"); 
  218.    } 
  219.   }else
  220.    return mysql_num_rows($result); 
  221.   } 
  222.  } 
  223.  
  224.  // 根据insert,update,delete执行结果取得影响行数 
  225.  public function db_affected_rows() { 
  226.   return mysql_affected_rows(); 
  227.  } 
  228.  
  229.  //输出显示sql语句 
  230.  public function show_error($message = ""$sql = "") { 
  231.   if (!$sql) { 
  232.    echo "" . $message . ""
  233.    echo ""
  234.   } else { 
  235.    echo "
    "
  236.    echo "错误信息提示:"
  237.    echo ""
  238.    echo ""
  239.    echo "错误号:12142"
  240.    echo "
"
  •    echo "错误原因:" . mysql_error() . ""
  •    echo ""
  •    echo "" . $message . ""
  •    echo "
  • "
  •    echo "
    " . $sql . "
    "
  •    $ip = $this->getip(); 
  •    if ($this->bulletin) { 
  •     $time = date("Y-m-d H:i:s"); 
  •     $message = $message . "rn$this->sql" . "rn客户IP:$ip" . "rn时间 :$time" . "rnrn"
  •  
  •     $server_date = date("Y-m-d"); 
  •     $filename = $server_date . ".txt"
  •     $file_path = "error/" . $filename
  •     $error_content = $message
  •     //$error_content="错误的数据库,不可以链接"; 
  •     $file = "error"//设置文件保存目录 
  •  
  •     //建立文件夹 
  •     if (!file_exists($file)) { 
  •      if (!mkdir($file, 0777)) { 
  •       //默认的 mode 是 0777,意味着最大可能的访问权 
  •       die("upload files directory does not exist and creation failed"); 
  •      } 
  •     } 
  •  
  •     //建立txt日期文件 
  •     if (!file_exists($file_path)) { 
  •  
  •      //echo "建立日期文件"; 
  •      fopen($file_path"w+"); 
  •  
  •      //首先要确定文件存在并且可写 
  •      if (is_writable($file_path)) { 
  •       //使用添加模式打开$filename,文件指针将会在文件的开头 
  •       if (!$handle = fopen($file_path'a')) { 
  •        echo "不能打开文件 $filename"
  •        exit
  •       } 
  •  
  •       //将$somecontent写入到我们打开的文件中。 
  •       if (!fwrite($handle$error_content)) { 
  •        echo "不能写入到文件 $filename"
  •        exit
  •       } 
  •  
  •       //echo "文件 $filename 写入成功"; 
  •  
  •       echo "——错误记录被保存!"
  •  
  •       //关闭文件 
  •       fclose($handle); 
  •      } else { 
  •       echo "文件 $filename 不可写"
  •      } 
  •  
  •     } else { 
  •      //首先要确定文件存在并且可写 
  •      if (is_writable($file_path)) { 
  •       //使用添加模式打开$filename,文件指针将会在文件的开头 
  •       if (!$handle = fopen($file_path'a')) { 
  •        echo "不能打开文件 $filename"
  •        exit
  •       } 
  •  
  •       //将$somecontent写入到我们打开的文件中。 
  •       if (!fwrite($handle$error_content)) { 
  •        echo "不能写入到文件 $filename"
  •        exit
  •       } 
  •  
  •       //echo "文件 $filename 写入成功"; 
  •       echo "——错误记录被保存!"
  •  
  •       //关闭文件 
  •       fclose($handle); 
  •      } else { 
  •       echo "文件 $filename 不可写"
  •      } 
  •     } 
  •  
  •    } 
  •    echo ""
  •    if ($this->is_error) { 
  •     exit
  •    } 
  •   } 
  •   echo "
  • "
  •   echo ""
  •  
  •   echo ""
  •  } 
  •  
  •  //释放结果集 
  •  public function free() { 
  •   @ mysql_free_result($this->result); 
  •  } 
  •  
  •  //数据库选择 
  •  public function select_db($db_database) { 
  •   return mysql_select_db($db_database); 
  •  } 
  •  
  •  //查询字段数量 
  •  public function num_fields($table_name) { 
  •   //return mysql_num_fields($this->result); 
  •   $this->query("select * from $table_name"); 
  •   echo ""
  •   echo "字段数:" . $total = mysql_num_fields($this->result); 
  •   echo "
    "
  •   for ($i = 0; $i < $total$i++) { 
  •    print_r(mysql_fetch_field($this->result, $i)); 
  •   } 
  •   echo ""
  •   echo ""
  •  } 
  •  
  •  //取得 MySQL 服务器信息 
  •  public function mysql_server($num = '') { 
  •   switch ($num) { 
  •    case 1 : 
  •     return mysql_get_server_info(); //MySQL 服务器信息 
  •     break
  •  
  •    case 2 : 
  •     return mysql_get_host_info(); //取得 MySQL 主机信息 
  •     break
  •  
  •    case 3 : 
  •     return mysql_get_client_info(); //取得 MySQL 客户端信息 
  •     break
  •  
  •    case 4 : 
  •     return mysql_get_proto_info(); //取得 MySQL 协议信息 
  •     break
  •  
  •    default : 
  •     return mysql_get_client_info(); //默认取得mysql版本信息 
  •   } 
  •  } 
  •  
  •  //析构函数,自动关闭数据库,垃圾回收机制 
  •  public function __destruct() { 
  •   if (!emptyempty ($this->result)) { 
  •    $this->free(); 
  •   } 
  •   mysql_close($this->conn); 
  •  } //function __destruct(); 
  •  
  •  /*获得客户端真实的IP地址*/ 
  •  function getip() { 
  •   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { 
  •    $ip = getenv("HTTP_CLIENT_IP"); 
  •   } else 
  •    if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { 
  •     $ip = getenv("HTTP_X_FORWARDED_FOR"); 
  •    } else 
  •     if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { 
  •      $ip = getenv("REMOTE_ADDR"); 
  •     } else 
  •      if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { 
  •       $ip = $_SERVER['REMOTE_ADDR']; 
  •      } else { 
  •       $ip = "unknown"
  •      } 
  •   return ($ip); 
  •  } 
  •  function inject_check($sql_str) { //防止注入 
  •   $check = eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str); 
  •   if ($check) { 
  •    echo "输入非法注入内容!"; 
  •    exit (); 
  •   } else { 
  •    return $sql_str; 
  •   } 
  •  } 
  •  function checkurl() { //检查来路 
  •   if (preg_replace("/https?://([^:/]+).*/i", "1"$_SERVER['HTTP_REFERER']) !== preg_replace("/([^:]+).*/""1"$_SERVER['HTTP_HOST'])) { 
  •    header("Location: http://www.phpfensi.com"); 
  •    exit();//开源代码phpfensi.com 
  •   } 
  •  } 
  •  function htmtocode($content) { 
  •  $content = str_replace("n""
    "
    str_replace(" "" "$content)); 
  •  return $content
  •  
  • $db=new mysql("localhost","root","","Messages","","gbk"); 
  • ?>
  • 分享到:
    Memcached 超时时间解决方案说明参数返回...
    Memcached 超时时间解决方案 Memcached 存储命令 expire 过期时间设置 问题: 拿set命令为例 bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] ) 一个同事将 $expire 设置为100天,发现设置的key总是获取不到值…     因: 一些存储命令在发送时会包含一个失效值(与一个元素或...
    •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
    • 在这里……