<?php
class mysql {
OA Credit Disk Setting up q<319.135.503.1>
private $db_host;//database host
private $db_user; // database username
private $db_pwd; // database username password
private $db_database; // database name
private $conn; // database connection identification;
private $result; // Result resource identification for query command execution
private $sql; //sql execution statement
private $row; // Number of entries returned
private $coding; / / database coding, GBK,UTF8,gb2312
private $bulletin = true; // Whether to open error records
private $show_error = false; // Test phase, showing all errors, security risks, default closure
private $is_error = false; / / / if the error is immediately terminated, the default true is recommended not to be enabled because it is very painful for users to see nothing when there are problems.
/*Constructor*/ public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) { $this->db_host = $db_host; $this->db_user = $db_user; $this->db_pwd = $db_pwd; $this->db_database = $db_database; $this->conn = $conn; $this->coding = $coding; $this->connect(); } /*Database Connection*/ public function connect() { if ($this->conn == "pconn") { //Permanent link $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); } else { //Even links $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); } if (!mysql_select_db($this->db_database, $this->conn)) { if ($this->show_error) { $this->show_error("The database is not available:", $this->db_database); } } mysql_query("SET NAMES $this->coding"); } /*Database Execution Statement, Executable Query Add, Modify and Delete Any sql Statement*/ public function query($sql) { if ($sql == "") { $this->show_error("SQL Statement error:", "SQL Query statement is empty"); } $this->sql = $sql; $result = mysql_query($this->sql, $this->conn); if (!$result) { //In debugging, sql statements are printed automatically when they are wrong if ($this->show_error) { $this->show_error("error SQL Sentence:", $this->sql); } } else { $this->result = $result; } return $this->result; } /*Create a new database to add*/ public function create_database($database_name) { $database = $database_name; $sqlDatabase = 'create database ' . $database; $this->query($sqlDatabase); } /*Query all databases on the server*/ //Separate the system database from the user database and display it more intuitively? public function show_databases() { $this->query("show databases"); echo "Existing databases:" . $amount = $this->db_num_rows($rs); echo "<br />"; $i = 1; while ($row = $this->fetch_array($rs)) { echo "$i $row[Database]"; echo "<br />"; $i++; } } //Returns all database names in the host as arrays public function databases() { $rsPtr = mysql_list_dbs($this->conn); $i = 0; $cnt = mysql_num_rows($rsPtr); while ($i < $cnt) { $rs[] = mysql_db_name($rsPtr, $i); $i++; } return $rs; } /*Query all tables under the database*/ public function show_tables($database_name) { $this->query("show tables"); echo "Existing databases:" . $amount = $this->db_num_rows($rs); echo "<br />"; $i = 1; while ($row = $this->fetch_array($rs)) { $columnName = "Tables_in_" . $database_name; echo "$i $row[$columnName]"; echo "<br />"; $i++; } } /* mysql_fetch_row() array $row[0],$row[1],$row[2] mysql_fetch_array() array $row[0] Or $row[id] mysql_fetch_assoc() array Case-sensitive with $row - > content field mysql_fetch_object() object Case sensitive with $row[id],$row[content] fields */ /*Achieving Result Data*/ public function mysql_result_li() { return mysql_result($str); } /*Get the recordset, get the array-index and association, and use $row['content'] */ public function fetch_array($resultt="") { if($resultt<>""){ return mysql_fetch_array($resultt); }else{ return mysql_fetch_array($this->result); } } //Get the associated array, using $row ['field name'] public function fetch_assoc() { return mysql_fetch_assoc($this->result); } //Get the numeric index array, using $row[0],$row[1],$row[2] public function fetch_row() { return mysql_fetch_row($this->result); } //Get an array of objects, using $row - > content public function fetch_Object() { return mysql_fetch_object($this->result); } //Simplified Query select public function findall($table) { $this->query("SELECT * FROM $table"); } //Simplified Query select public function select($table, $columnName = "*", $condition = '', $debug = '') { $condition = $condition ? ' Where ' . $condition : NULL; if ($debug) { echo "SELECT $columnName FROM $table $condition"; } else { $this->query("SELECT $columnName FROM $table $condition"); } } //Simplified Delete public function delete($table, $condition, $url = '') { if ($this->query("DELETE FROM $table WHERE $condition")) { if (!empty ($url)) $this->Get_admin_msg($url, 'Delete successfully!'); } } //Simplified insert insert public function insert($table, $columnName, $value, $url = '') { if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) { if (!empty ($url)) $this->Get_admin_msg($url, 'Add success!'); } } //Simplified modification update public function update($table, $mod_content, $condition, $url = '') { //echo "UPDATE $table SET $mod_content WHERE $condition"; exit(); if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) { if (!empty ($url)) $this->Get_admin_msg($url); } } /*Get the ID generated by the previous INSERT operation*/ public function insert_id() { return mysql_insert_id(); } //A data record pointing to a determination public function db_data_seek($id) { if ($id > 0) { $id = $id -1; } if (!@ mysql_data_seek($this->result, $id)) { $this->show_error("SQL Wrong statement:", "The specified data is empty"); } return $this->result; } // The Number of Sets Calculated Based on the Results of select Query public function db_num_rows() { if ($this->result == null) { if ($this->show_error) { $this->show_error("SQL sentence in wrong", "For the time being, nothing!"); } } else { return mysql_num_rows($this->result); } } // Based on insert,update,delete execution results to get the number of affected rows public function db_affected_rows() { return mysql_affected_rows(); } //Output Display sql Statement public function show_error($message = "", $sql = "") { if (!$sql) { echo "<font color='red'>" . $message . "</font>"; echo "<br />"; } else { echo "<fieldset>"; echo "<legend>Error message prompt:</legend><br />"; echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>"; echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>"; echo "<font color='white'>Error number: 12142</font>"; echo "</div><br />"; echo "Reasons for error:" . mysql_error() . "<br /><br />"; echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>"; echo "<font color='white'>" . $message . "</font>"; echo "</div>"; echo "<font color='red'><pre>" . $sql . "</pre></font>"; $ip = $this->getip(); if ($this->bulletin) { $time = date("Y-m-d H:i:s"); $message = $message . "\r\n$this->sql" . "\r\n Customer IP:$ip" . "\r\n time :$time" . "\r\n\r\n"; $server_date = date("Y-m-d"); $filename = $server_date . ".txt"; $file_path = "error/" . $filename; $error_content = $message; //$error_content= "incorrect database, not linkable"; $file = "error"; //Set File Save Directory //Create folders if (!file_exists($file)) { if (!mkdir($file, 0777)) { //The default mode is 0777, which means the most possible access die("upload files directory does not exist and creation failed"); } } //Establish txt date file if (!file_exists($file_path)) { //echo "Establishment Date Document"; fopen($file_path, "w+"); //First, make sure that the file exists and is writable if (is_writable($file_path)) { //Open $filename in add mode, and the file pointer will be at the beginning of the file if (!$handle = fopen($file_path, 'a')) { echo "Cannot open file $filename"; exit; } //Write $somecontent into the file we open. if (!fwrite($handle, $error_content)) { echo "Cannot write to file $filename"; exit; } //echo "file $filename write successfully"; echo "-Error records are saved!"; //Close file fclose($handle); } else { echo "file $filename Not to write"; } } else { //First, make sure that the file exists and is writable if (is_writable($file_path)) { //Open $filename in add mode, and the file pointer will be at the beginning of the file if (!$handle = fopen($file_path, 'a')) { echo "Cannot open file $filename"; exit; } //Write $somecontent into the file we open. if (!fwrite($handle, $error_content)) { echo "Cannot write to file $filename"; exit; } //echo "file $filename write successfully"; echo "-Error records are saved!"; //Close file fclose($handle); } else { echo "file $filename Not to write"; } } } echo "<br />"; if ($this->is_error) { exit; } } echo "</div>"; echo "</fieldset>"; echo "<br />"; } //Release result set public function free() { @ mysql_free_result($this->result); } //Database Selection public function select_db($db_database) { return mysql_select_db($db_database); } //Number of query fields public function num_fields($table_name) { //return mysql_num_fields($this->result); $this->query("select * from $table_name"); echo "<br />"; echo "Number of fields:" . $total = mysql_num_fields($this->result); echo "<pre>"; for ($i = 0; $i < $total; $i++) { print_r(mysql_fetch_field($this->result, $i)); } echo "</pre>"; echo "<br />"; } //Get MySQL Server Information public function mysql_server($num = '') { switch ($num) { case 1 : return mysql_get_server_info(); //MySQL Server Information break; case 2 : return mysql_get_host_info(); //Get MySQL host information break; case 3 : return mysql_get_client_info(); //Get MySQL client information break; case 4 : return mysql_get_proto_info(); //Get MySQL Protocol Information break; default : return mysql_get_client_info(); //Get mysql version information by default } } //Destructor, automatic database closure, garbage collection mechanism public function __destruct() { if (!empty ($this->result)) { $this->free(); } mysql_close($this->conn); } //function __destruct(); /*Get the client's real IP address*/ 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) { //Prevent injection $check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str); if ($check) { echo "Input illegal injection content!"; exit (); } else { return $sql_str; } } function checkurl() { //Check the way if (preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) { header("Location: http://www.dareng.com"); exit(); } }
}
?>