QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

PHP基础

完整的PHP MYSQL数据库类

 admin  2017-03-05 11:55:32
根据部分用户的反馈,该内容有部分错误,仅供大家学习使用!
  1. <?php  
  2. class mysql {  
  3.     private $db_host; //数据库主机  
  4.     private $db_user; //数据库用户名  
  5.     private $db_pwd; //数据库用户名密码  
  6.     private $db_database; //数据库名  
  7.     private $conn; //数据库连接标识;  
  8.     private $result; //执行query命令的结果资源标识  
  9.     private $sql; //sql执行语句  
  10.     private $row; //返回的条目数  
  11.     private $coding; //数据库编码,GBK,UTF8,gb2312  
  12.     private $bulletin = true//是否开启错误记录  
  13.     private $show_error = false//测试阶段,显示所有错误,具有安全隐患,默认关闭  
  14.     private $is_error = false//发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的  
  15.    
  16.     /*构造函数*/  
  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.     /*数据库连接*/  
  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("数据库不可用:", $this->db_database);  
  40.             }  
  41.         }  
  42.         mysql_query("SET NAMES $this->coding");  
  43.     }  
  44.    
  45.     /*数据库执行语句,可执行查询添加修改删除等任何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.     /*创建添加新的数据库*/  
  66.     public function create_database($database_name) {  
  67.         $database = $database_name;  
  68.         $sqlDatabase = 'create database ' . $database;  
  69.         $this->query($sqlDatabase);  
  70.     }  
  71.    
  72.     /*查询服务器所有数据库*/  
  73.     //将系统数据库与用户数据库分开,更直观的显示?  
  74.     public function show_databases() {  
  75.         $this->query("show databases");  
  76.         echo "现有数据库:" . $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.     //以数组形式返回主机中所有数据库名  
  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.     /*查询数据库下所有的表*/  
  99.     public function show_tables($database_name) {  
  100.         $this->query("show tables");  
  101.         echo "现有数据库:" . $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.     /*取得结果数据*/  
  120.     public function mysql_result_li() {  
  121.         return mysql_result($str);  
  122.     }  
  123.    
  124.     /*取得记录集,获取数组-索引和关联,使用$row['content'] */  
  125.     public function fetch_array($resultt="") {  
  126.         if($resultt<>""){  
  127.             return mysql_fetch_array($resultt);  
  128.         }else{  
  129.         return mysql_fetch_array($this->result);  
  130.         }  
  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 (!empty ($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 (!empty ($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 (!empty ($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.    
  215.     // 根据insert,update,delete执行结果取得影响行数  
  216.     public function db_affected_rows() {  
  217.         return mysql_affected_rows();  
  218.     }  
  219.    
  220.     //输出显示sql语句  
  221.     public function show_error($message = "", $sql = "") {  
  222.         if (!$sql) {  
  223.             echo "<font color='red'>" . $message . "</font>";  
  224.             echo "<br />";  
  225.         } else {  
  226.             echo "<fieldset>";  
  227.             echo "<legend>错误信息提示:</legend><br />";  
  228.             echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";  
  229.             echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";  
  230.             echo "<font color='white'>错误号:12142</font>";  
  231.             echo "</div><br />";  
  232.             echo "错误原因:" . mysql_error() . "<br /><br />";  
  233.             echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";  
  234.             echo "<font color='white'>" . $message . "</font>";  
  235.             echo "</div>";  
  236.             echo "<font color='red'><pre>" . $sql . "</pre></font>";  
  237.             $ip = $this->getip();  
  238.             if ($this->bulletin) {  
  239.                 $time = date("Y-m-d H:i:s");  
  240.                 $message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";  
  241.    
  242.                 $server_date = date("Y-m-d");  
  243.                 $filename = $server_date . ".txt";  
  244.                 $file_path = "error/" . $filename;  
  245.                 $error_content = $message;  
  246.                 //$error_content="错误的数据库,不可以链接";  
  247.                 $file = "error"//设置文件保存目录  
  248.    
  249.                 //建立文件夹  
  250.                 if (!file_exists($file)) {  
  251.                     if (!mkdir($file, 0777)) {  
  252.                         //默认的 mode 是 0777,意味着最大可能的访问权  
  253.                         die("upload files directory does not exist and creation failed");  
  254.                     }  
  255.                 }  
  256.    
  257.                 //建立txt日期文件  
  258.                 if (!file_exists($file_path)) {  
  259.    
  260.                     //echo "建立日期文件";  
  261.                     fopen($file_path, "w+");  
  262.    
  263.                     //首先要确定文件存在并且可写  
  264.                     if (is_writable($file_path)) {  
  265.                         //使用添加模式打开$filename,文件指针将会在文件的开头  
  266.                         if (!$handle = fopen($file_path, 'a')) {  
  267.                             echo "不能打开文件 $filename";  
  268.                             exit;  
  269.                         }  
  270.    
  271.                         //将$somecontent写入到我们打开的文件中。  
  272.                         if (!fwrite($handle, $error_content)) {  
  273.                             echo "不能写入到文件 $filename";  
  274.                             exit;  
  275.                         }  
  276.    
  277.                         //echo "文件 $filename 写入成功";  
  278.    
  279.                         echo "——错误记录被保存!";  
  280.    
  281.                         //关闭文件  
  282.                         fclose($handle);  
  283.                     } else {  
  284.                         echo "文件 $filename 不可写";  
  285.                     }  
  286.    
  287.                 } else {  
  288.                     //首先要确定文件存在并且可写  
  289.                     if (is_writable($file_path)) {  
  290.                         //使用添加模式打开$filename,文件指针将会在文件的开头  
  291.                         if (!$handle = fopen($file_path, 'a')) {  
  292.                             echo "不能打开文件 $filename";  
  293.                             exit;  
  294.                         }  
  295.    
  296.                         //将$somecontent写入到我们打开的文件中。  
  297.                         if (!fwrite($handle, $error_content)) {  
  298.                             echo "不能写入到文件 $filename";  
  299.                             exit;  
  300.                         }  
  301.    
  302.                         //echo "文件 $filename 写入成功";  
  303.                         echo "——错误记录被保存!";  
  304.    
  305.                         //关闭文件  
  306.                         fclose($handle);  
  307.                     } else {  
  308.                         echo "文件 $filename 不可写";  
  309.                     }  
  310.                 }  
  311.    
  312.             }  
  313.             echo "<br />";  
  314.             if ($this->is_error) {  
  315.                 exit;  
  316.             }  
  317.         }  
  318.         echo "</div>";  
  319.         echo "</fieldset>";  
  320.    
  321.         echo "<br />";  
  322.     }  
  323.    
  324.     //释放结果集  
  325.     public function free() {  
  326.         @ mysql_free_result($this->result);  
  327.     }  
  328.    
  329.     //数据库选择  
  330.     public function select_db($db_database) {  
  331.         return mysql_select_db($db_database);  
  332.     }  
  333.    
  334.     //查询字段数量  
  335.     public function num_fields($table_name) {  
  336.         //return mysql_num_fields($this->result);  
  337.         $this->query("select * from $table_name");  
  338.         echo "<br />";  
  339.         echo "字段数:" . $total = mysql_num_fields($this->result);  
  340.         echo "<pre>";  
  341.         for ($i = 0; $i < $total; $i++) {  
  342.             print_r(mysql_fetch_field($this->result, $i));  
  343.         }  
  344.         echo "</pre>";  
  345.         echo "<br />";  
  346.     }  
  347.    
  348.     //取得 MySQL 服务器信息  
  349.     public function mysql_server($num = '') {  
  350.         switch ($num) {  
  351.             case 1 :  
  352.                 return mysql_get_server_info(); //MySQL 服务器信息  
  353.                 break;  
  354.    
  355.             case 2 :  
  356.                 return mysql_get_host_info(); //取得 MySQL 主机信息  
  357.                 break;  
  358.    
  359.             case 3 :  
  360.                 return mysql_get_client_info(); //取得 MySQL 客户端信息  
  361.                 break;  
  362.    
  363.             case 4 :  
  364.                 return mysql_get_proto_info(); //取得 MySQL 协议信息  
  365.                 break;  
  366.    
  367.             default :  
  368.                 return mysql_get_client_info(); //默认取得mysql版本信息  
  369.         }  
  370.     }  
  371.    
  372.     //析构函数,自动关闭数据库,垃圾回收机制  
  373.     public function __destruct() {  
  374.         if (!empty ($this->result)) {  
  375.             $this->free();  
  376.         }  
  377.         mysql_close($this->conn);  
  378.     } //function __destruct();  
  379.    
  380.     /*获得客户端真实的IP地址*/  
  381.     function getip() {  
  382.         if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {  
  383.             $ip = getenv("HTTP_CLIENT_IP");  
  384.         } else  
  385.             if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {  
  386.                 $ip = getenv("HTTP_X_FORWARDED_FOR");  
  387.             } else  
  388.                 if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {  
  389.                     $ip = getenv("REMOTE_ADDR");  
  390.                 } else  
  391.                     if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {  
  392.                         $ip = $_SERVER['REMOTE_ADDR'];  
  393.                     } else {  
  394.                         $ip = "unknown";  
  395.                     }  
  396.         return ($ip);  
  397.     }  
  398.     function inject_check($sql_str) { //防止注入  
  399.         $check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);  
  400.         if ($check) {  
  401.             echo "输入非法注入内容!";  
  402.             exit ();  
  403.         } else {  
  404.             return $sql_str;  
  405.         }  
  406.     }  
  407.     function checkurl() { //检查来路  
  408.         if (preg_replace("/https?:\/\/([^\:\/]+).*/i""\\1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/""\\1", $_SERVER['HTTP_HOST'])) {  
  409.             header("Location: http://www.dareng.com");  
  410.             exit();  
  411.         }  
  412.     }  
  413.    
  414. }  
  415. ?>  

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《完整的PHP MYSQL数据库类》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/html/php/680.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序