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

首頁 > 語言 > PHP > 正文

php mysql完整數(shù)據(jù)庫連接類

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉木萨尔县| 资中县| 丘北县| 济源市| 临沭县| 绩溪县| 安塞县| 栾川县| 博野县| 汨罗市| 大名县| 隆昌县| 锦州市| 大宁县| 丹棱县| 珲春市| 建德市| 忻城县| 大余县| 淳化县| 阜平县| 于都县| 江华| 申扎县| 喀喇沁旗| 宿松县| 柳河县| 新巴尔虎左旗| 呼伦贝尔市| 樟树市| 沙田区| 越西县| 南康市| 鄂托克旗| 福海县| 吴堡县| 运城市| 宣汉县| 淅川县| 黄浦区| 内丘县|