国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

mysql數(shù)據(jù)庫連接操作類

2024-09-04 11:44:14
字體:
來源:轉載
供稿:網友

分享一篇可以記錄錯誤日志的mysql數(shù)據(jù)庫連接類文件,實例代碼如下:

  1. <?php 
  2. class mysql { 
  3.  private $db_host//數(shù)據(jù)庫主機 
  4.  private $db_user//數(shù)據(jù)庫用戶名 
  5.  private $db_pwd//數(shù)據(jù)庫用戶名密碼 
  6.  private $db_database//數(shù)據(jù)庫名 
  7.  private $conn//數(shù)據(jù)庫連接標識; 
  8.  private $result//執(zhí)行query命令的結果資源標識 
  9.  private $sql//sql執(zhí)行語句 
  10.  private $row//返回的條目數(shù) 
  11.  private $coding//數(shù)據(jù)庫編碼,GBK,UTF8,gb2312 
  12.  private $bulletin = true; //是否開啟錯誤記錄 
  13.  private $show_error = true; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉 
  14.  private $is_error = false; //發(fā)現(xiàn)錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什么也看不到是很苦惱的 
  15.  
  16.  /*構造函數(shù)*/ 
  17.  public function __construct($db_host$db_user$db_pwd$db_database$conn$coding) { 
  18.   $this->db_host = $db_host
  19.   $this->db_user = $db_user
  20.   $this->db_pwd = $db_pwd
  21.   $this->db_database = $db_database
  22.   $this->conn = $conn
  23.   $this->coding = $coding
  24.   $this->connect(); 
  25.  } 
  26.  
  27.  /*數(shù)據(jù)庫連接*/ 
  28.  public function connect() { 
  29.   if ($this->conn == "pconn") { 
  30.    //永久鏈接 
  31.    $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); 
  32.   } else { 
  33.    //即使鏈接 
  34.    $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); 
  35.   } 
  36.  
  37.   if (!mysql_select_db($this->db_database, $this->conn)) { 
  38.    if ($this->show_error) { 
  39.     $this->show_error("數(shù)據(jù)庫不可用:"$this->db_database); 
  40.    } 
  41.   } 
  42.   mysql_query("SET NAMES $this->coding"); 
  43.  } 
  44.  
  45.  /*數(shù)據(jù)庫執(zhí)行語句,可執(zhí)行查詢添加修改刪除等任何sql語句*/ 
  46.  public function query($sql) { 
  47.   if ($sql == "") { 
  48.    $this->show_error("SQL語句錯誤:""SQL查詢語句為空"); 
  49.   } 
  50.   $this->sql = $sql
  51.  
  52.   $result = mysql_query($this->sql, $this->conn); 
  53.  
  54.   if (!$result) { 
  55.    //調試中使用,sql語句出錯時會自動打印出來 
  56.    if ($this->show_error) { 
  57.     $this->show_error("錯誤SQL語句:"$this->sql); 
  58.    } 
  59.   } else { 
  60.    $this->result = $result
  61.   } 
  62.   return $this->result; 
  63.  } 
  64.  
  65.  /*創(chuàng)建添加新的數(shù)據(jù)庫*/ 
  66.  public function create_database($database_name) { 
  67.   $database = $database_name
  68.   $sqlDatabase = 'create database ' . $database
  69.   $this->query($sqlDatabase); 
  70.  } 
  71.  
  72.  /*查詢服務器所有數(shù)據(jù)庫*/ 
  73.  //將系統(tǒng)數(shù)據(jù)庫與用戶數(shù)據(jù)庫分開,更直觀的顯示? 
  74.  public function show_databases() { 
  75.   $this->query("show databases"); 
  76.   echo "現(xiàn)有數(shù)據(jù)庫:" . $amount = $this->db_num_rows($rs); 
  77.   echo "<br />"
  78.   $i = 1; 
  79.   while ($row = $this->fetch_array($rs)) { 
  80.    echo "$i $row[Database]"
  81.    echo "<br />"
  82.    $i++; 
  83.   } 
  84.  } 
  85.  
  86.  //以數(shù)組形式返回主機中所有數(shù)據(jù)庫名 
  87.  public function databases() { 
  88.   $rsPtr = mysql_list_dbs($this->conn); 
  89.   $i = 0; 
  90.   $cnt = mysql_num_rows($rsPtr); 
  91.   while ($i < $cnt) { 
  92.    $rs[] = mysql_db_name($rsPtr$i); 
  93.    $i++; 
  94.   } 
  95.   return $rs
  96.  } 
  97.  
  98.  /*查詢數(shù)據(jù)庫下所有的表*/ 
  99.  public function show_tables($database_name) { 
  100.   $this->query("show tables"); 
  101.   echo "現(xiàn)有數(shù)據(jù)庫:" . $amount = $this->db_num_rows($rs); 
  102.   echo "<br />"
  103.   $i = 1; 
  104.   while ($row = $this->fetch_array($rs)) { 
  105.    $columnName = "Tables_in_" . $database_name
  106.    echo "$i $row[$columnName]"
  107.    echo "<br />"
  108.    $i++; 
  109.   } 
  110.  } 
  111.  
  112.  /* 
  113.  mysql_fetch_row()    array  $row[0],$row[1],$row[2] 
  114.  mysql_fetch_array()  array  $row[0] 或 $row[id] 
  115.  mysql_fetch_assoc()  array  用$row->content 字段大小寫敏感 
  116.  mysql_fetch_object() object 用$row[id],$row[content] 字段大小寫敏感 
  117.  */ 
  118.  
  119.  /*取得結果數(shù)據(jù)*/ 
  120.  public function mysql_result_li() { 
  121.   return mysql_result($str); 
  122.  } 
  123.  
  124.  /*取得記錄集,獲取數(shù)組-索引和關聯(lián),使用$row['content'] */ 
  125.  
  126.  public function fetch_array() { 
  127.   return mysql_fetch_array($this->result); 
  128.  } 
  129.  
  130. // public function fetch_array($query) { 
  131. //  return mysql_fetch_array($query); 
  132. // } 
  133.  
  134.  //獲取關聯(lián)數(shù)組,使用$row['字段名'] 
  135.  public function fetch_assoc() { 
  136.   return mysql_fetch_assoc($this->result); 
  137.  } 
  138.  
  139.  //獲取數(shù)字索引數(shù)組,使用$row[0],$row[1],$row[2] 
  140.  public function fetch_row() { 
  141.   return mysql_fetch_row($this->result); 
  142.  } 
  143.  
  144.  //獲取對象數(shù)組,使用$row->content 
  145.  public function fetch_Object() { 
  146.   return mysql_fetch_object($this->result); 
  147.  } 
  148.  
  149.  //簡化查詢select 
  150.  public function findall($table) { 
  151.   $this->query("SELECT * FROM $table"); 
  152.  } 
  153.  
  154.  //簡化查詢select 
  155.  public function select($table$columnName = "*"$condition = ''$debug = '') { 
  156.   $condition = $condition ? ' Where ' . $condition : NULL; 
  157.   if ($debug) { 
  158.    echo "SELECT $columnName FROM $table $condition"
  159.   } else { 
  160.    $this->query("SELECT $columnName FROM $table $condition"); 
  161.   } 
  162.  } 
  163.  
  164.  //簡化刪除del 
  165.  public function delete($table$condition$url = '') { 
  166.   if ($this->query("DELETE FROM $table WHERE $condition")) { 
  167.    if (!emptyempty ($url)) 
  168.     $this->Get_admin_msg($url'刪除成功!'); 
  169.   } 
  170.  } 
  171.  
  172.  //簡化插入insert 
  173.  public function insert($table$columnName$value$url = '') { 
  174.   if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) { 
  175.    if (!emptyempty ($url)) 
  176.     $this->Get_admin_msg($url'添加成功!'); 
  177.   } 
  178.  } 
  179.  
  180.  //簡化修改update 
  181.  public function update($table$mod_content$condition$url = '') { 
  182.   //echo "UPDATE $table SET $mod_content WHERE $condition"; exit(); 
  183.   if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) { 
  184.    if (!emptyempty ($url)) 
  185.     $this->Get_admin_msg($url); 
  186.   } 
  187.  } 
  188.  
  189.  /*取得上一步 INSERT 操作產生的 ID*/ 
  190.  public function insert_id() { 
  191.   return mysql_insert_id(); 
  192.  } 
  193.  
  194.  //指向確定的一條數(shù)據(jù)記錄 
  195.  public function db_data_seek($id) { 
  196.   if ($id > 0) { 
  197.    $id = $id -1; 
  198.   } 
  199.   if (!@ mysql_data_seek($this->result, $id)) { 
  200.    $this->show_error("SQL語句有誤:""指定的數(shù)據(jù)為空"); 
  201.   } 
  202.   return $this->result; 
  203.  } 
  204.  
  205.  // 根據(jù)select查詢結果計算結果集條數(shù) 
  206. /* public function db_num_rows() { 
  207.   if ($this->result == null) { 
  208.    if ($this->show_error) { 
  209.     $this->show_error("SQL語句錯誤", "暫時為空,沒有任何內容!"); 
  210.    } 
  211.   } else { 
  212.    return mysql_num_rows($this->result); 
  213.   } 
  214.  }*/ 
  215.  public function db_num_rows($result){ 
  216.   if($result==null){ 
  217.   if ($this->show_error) { 
  218.     $this->show_error("SQL語句錯誤""暫時為空,沒有任何內容!"); 
  219.    } 
  220.   }else
  221.    return mysql_num_rows($result); 
  222.   } 
  223.  } 
  224.  
  225.  // 根據(jù)insert,update,delete執(zhí)行結果取得影響行數(shù) 
  226.  public function db_affected_rows() { 
  227.   return mysql_affected_rows(); 
  228.  } 
  229.  
  230.  //輸出顯示sql語句 
  231.  public function show_error($message = ""$sql = "") { 
  232.   if (!$sql) { 
  233.    echo "<font color='red'>" . $message . "</font>"
  234.    echo "<br />"
  235.   } else { 
  236.    echo "<fieldset>"
  237.    echo "<legend>錯誤信息提示:</legend><br />"
  238.    echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>"
  239.    echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>"
  240.    echo "<font color='white'>錯誤號:12142</font>"
  241.    echo "</div><br />"
  242.    echo "錯誤原因:" . mysql_error() . "<br /><br />"
  243.    echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>"
  244.    echo "<font color='white'>" . $message . "</font>"
  245.    echo "</div>"
  246.    echo "<font color='red'><pre>" . $sql . "</pre></font>"
  247.    $ip = $this->getip(); 
  248.    if ($this->bulletin) { 
  249.     $time = date("Y-m-d H:i:s"); 
  250.     $message = $message . "rn$this->sql" . "rn客戶IP:$ip" . "rn時間 :$time" . "rnrn"
  251.  
  252.     $server_date = date("Y-m-d"); 
  253.     $filename = $server_date . ".txt"
  254.     $file_path = "error/" . $filename
  255.     $error_content = $message
  256.     //$error_content="錯誤的數(shù)據(jù)庫,不可以鏈接"; 
  257.     $file = "error"//設置文件保存目錄 
  258.  
  259.     //建立文件夾 
  260.     if (!file_exists($file)) { 
  261.      if (!mkdir($file, 0777)) { 
  262.       //默認的 mode 是 0777,意味著最大可能的訪問權 
  263.       die("upload files directory does not exist and creation failed"); 
  264.      } 
  265.     } 
  266.  
  267.     //建立txt日期文件 
  268.     if (!file_exists($file_path)) { 
  269.  
  270.      //echo "建立日期文件"; 
  271.      fopen($file_path"w+"); 
  272.  
  273.      //首先要確定文件存在并且可寫 
  274.      if (is_writable($file_path)) { 
  275.       //使用添加模式打開$filename,文件指針將會在文件的開頭 
  276.       if (!$handle = fopen($file_path'a')) { 
  277.        echo "不能打開文件 $filename"
  278.        exit
  279.       } 
  280.  
  281.       //將$somecontent寫入到我們打開的文件中。 
  282.       if (!fwrite($handle$error_content)) { 
  283.        echo "不能寫入到文件 $filename"
  284.        exit
  285.       } 
  286.  
  287.       //echo "文件 $filename 寫入成功"; 
  288.  
  289.       echo "——錯誤記錄被保存!"
  290.  
  291.       //關閉文件 
  292.       fclose($handle); 
  293.      } else { 
  294.       echo "文件 $filename 不可寫"
  295.      } 
  296.  
  297.     } else { 
  298.      //首先要確定文件存在并且可寫 
  299.      if (is_writable($file_path)) { 
  300.       //使用添加模式打開$filename,文件指針將會在文件的開頭 
  301.       if (!$handle = fopen($file_path'a')) { 
  302.        echo "不能打開文件 $filename"
  303.        exit
  304.       } 
  305.  
  306.       //將$somecontent寫入到我們打開的文件中。 
  307.       if (!fwrite($handle$error_content)) { 
  308.        echo "不能寫入到文件 $filename"
  309.        exit
  310.       } 
  311.  
  312.       //echo "文件 $filename 寫入成功"; 
  313.       echo "——錯誤記錄被保存!"
  314.  
  315.       //關閉文件 
  316.       fclose($handle); 
  317.      } else { 
  318.       echo "文件 $filename 不可寫"
  319.      } 
  320.     } 
  321.  
  322.    } 
  323.    echo "<br />"
  324.    if ($this->is_error) { 
  325.     exit
  326.    } 
  327.   } 
  328.   echo "</div>"
  329.   echo "</fieldset>"
  330.  
  331.   echo "<br />"
  332.  } 
  333.  
  334.  //釋放結果集 
  335.  public function free() { 
  336.   @ mysql_free_result($this->result); 
  337.  } 
  338.  
  339.  //數(shù)據(jù)庫選擇 
  340.  public function select_db($db_database) { 
  341.   return mysql_select_db($db_database); 
  342.  } 
  343.  
  344.  //查詢字段數(shù)量 
  345.  public function num_fields($table_name) { 
  346.   //return mysql_num_fields($this->result); 
  347.   $this->query("select * from $table_name"); 
  348.   echo "<br />"
  349.   echo "字段數(shù):" . $total = mysql_num_fields($this->result); 
  350.   echo "<pre>"
  351.   for ($i = 0; $i < $total$i++) { 
  352.    print_r(mysql_fetch_field($this->result, $i)); 
  353.   } 
  354.   echo "</pre>"
  355.   echo "<br />"
  356.  } 
  357.  
  358.  //取得 MySQL 服務器信息 
  359.  public function mysql_server($num = '') { 
  360.   switch ($num) { 
  361.    case 1 : 
  362.     return mysql_get_server_info(); //MySQL 服務器信息 
  363.     break
  364.  
  365.    case 2 : 
  366.     return mysql_get_host_info(); //取得 MySQL 主機信息 
  367.     break
  368.  
  369.    case 3 : 
  370.     return mysql_get_client_info(); //取得 MySQL 客戶端信息 
  371.     break
  372.  
  373.    case 4 : 
  374.     return mysql_get_proto_info(); //取得 MySQL 協(xié)議信息 
  375.     break
  376.  
  377.    default : 
  378.     return mysql_get_client_info(); //默認取得mysql版本信息 
  379.   } 
  380.  } 
  381.  
  382.  //析構函數(shù),自動關閉數(shù)據(jù)庫,垃圾回收機制 
  383.  public function __destruct() { 
  384.   if (!emptyempty ($this->result)) { 
  385.    $this->free(); 
  386.   } 
  387.   mysql_close($this->conn); 
  388.  } //function __destruct(); 
  389.  
  390.  /*獲得客戶端真實的IP地址*/ 
  391.  function getip() { 
  392.   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { 
  393.    $ip = getenv("HTTP_CLIENT_IP"); 
  394.   } else 
  395.    if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { 
  396.     $ip = getenv("HTTP_X_FORWARDED_FOR"); 
  397.    } else 
  398.     if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { 
  399.      $ip = getenv("REMOTE_ADDR"); 
  400.     } else 
  401.      if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { 
  402.       $ip = $_SERVER['REMOTE_ADDR']; 
  403.      } else { 
  404.       $ip = "unknown"
  405.      } 
  406.   return ($ip); 
  407.  } 
  408.  function inject_check($sql_str) { //防止注入 
  409.   $check = eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str); 
  410.   if ($check) { 
  411.    echo "輸入非法注入內容!"; 
  412.    exit (); 
  413.   } else { 
  414.    return $sql_str; 
  415.   } 
  416.  } 
  417.  function checkurl() { //檢查來路 
  418.   if (preg_replace("/https?://([^:/]+).*/i", "/1"$_SERVER['HTTP_REFERER']) !== preg_replace("/([^:]+).*/""/1"$_SERVER['HTTP_HOST'])) { 
  419.    header("Location: http://www.survivalescaperooms.com"); 
  420.    exit();//開源代碼Vevb.com 
  421.   } 
  422.  } 
  423.  function htmtocode($content) { 
  424.  $content = str_replace("n""<br>"str_replace(" ""&nbsp;"$content)); 
  425.  return $content
  426.  
  427. $db=new mysql("localhost","root","","Messages","","gbk"); 
  428. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安康市| 广宗县| 健康| 余庆县| 泰安市| SHOW| 尼木县| 崇明县| 屏东市| 贵溪市| 如皋市| 磐石市| 平泉县| 福州市| 新津县| 道孚县| 社旗县| 眉山市| 紫云| 木兰县| 田阳县| 鹤岗市| 高青县| 大邑县| 义乌市| 普洱| 南通市| 乐清市| 福海县| 铜川市| 常山县| 镶黄旗| 凌源市| 绍兴县| 台东县| 永仁县| 灵川县| 尉犁县| 淮南市| 谢通门县| 洞口县|