php mysql完整数据库连接类 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- class mysql {
- private $db_host;
- private $db_user;
- private $db_pwd;
- private $db_database;
- private $conn;
- private $result;
- private $sql;
- private $row;
- private $coding;
- private $bulletin = true;
- private $show_error = false;
- private $is_error = false;
-
-
- 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();
- }
-
-
- public function connect() {
- if ($this->conn == "pconn") {
-
- $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
- } else {
-
- $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("数据库不可用:", $this->db_database);
- }
- }
- mysql_query("set names $this->coding");
- }
-
-
- public function query($sql) {
- if ($sql == "") {
- $this->show_error("sql语句错误:", "sql查询语句为空");
- }
- $this->sql = $sql;
-
- $result = mysql_query($this->sql, $this->conn);
-
- if (!$result) {
-
- if ($this->show_error) {
- $this->show_error("错误sql语句:", $this->sql);
- }
- } else {
- $this->result = $result;
- }
- return $this->result;
- }
-
-
- public function create_database($database_name) {
- $database = $database_name;
- $sqldatabase = 'create database ' . $database;
- $this->query($sqldatabase);
- }
-
-
-
- public function show_databases() {
- $this->query("show databases");
- echo "现有数据库:" . $amount = $this->db_num_rows($rs);
- echo "<br />";
- $i = 1;
- while ($row = $this->fetch_array($rs)) {
- echo "$i $row[database]";
- echo "<br />";
- $i++;
- }
- }
-
-
- 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;
- }
-
-
- public function show_tables($database_name) {
- $this->query("show tables");
- echo "现有数据库:" . $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++;
- }
- }
-
-
-
-
-
-
-
-
-
- public function mysql_result_li() {
- return mysql_result($str);
- }
-
-
- public function fetch_array($resultt="") {
- if($resultt<>""){
- return mysql_fetch_array($resultt);
- }else{
- return mysql_fetch_array($this->result);
- }
- }
-
-
- public function fetch_assoc() {
- return mysql_fetch_assoc($this->result);
- }
-
-
- public function fetch_row() {
- return mysql_fetch_row($this->result);
- }
-
-
- public function fetch_object() {
- return mysql_fetch_object($this->result);
- }
-
-
- public function findall($table) {
- $this->query("select * from $table");
- }
-
-
- 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");
- }
- }
-
-
- public function delete($table, $condition, $url = '') {
- if ($this->query("delete from $table where $condition")) {
- if (!emptyempty ($url))
- $this->get_admin_msg($url, '删除成功!');
- }
- }
-
-
- public function insert($table, $columnname, $value, $url = '') {
- if ($this->query("insert into $table ($columnname) values ($value)")) {
- if (!emptyempty ($url))
- $this->get_admin_msg($url, '添加成功!');
- }
- }
-
-
- public function update($table, $mod_content, $condition, $url = '') {
-
- if ($this->query("update $table set $mod_content where $condition")) {
- if (!emptyempty ($url))
- $this->get_admin_msg($url);
- }
- }
-
-
- public function insert_id() {
- return mysql_insert_id();
- }
-
-
- public function db_data_seek($id) {
- if ($id > 0) {
- $id = $id -1;
- }
- if (!@ mysql_data_seek($this->result, $id)) {
- $this->show_error("sql语句有误:", "指定的数据为空");
- }
- return $this->result;
- }
-
-
- public function db_num_rows() {
- if ($this->result == null) {
- if ($this->show_error) {
- $this->show_error("sql语句错误", "暂时为空,没有任何内容!");
- }
- } else {
- return mysql_num_rows($this->result);
- }
- }
-
-
- public function db_affected_rows() {
- return mysql_affected_rows();
- }
-
-
- public function show_error($message = "", $sql = "") {
- if (!$sql) {
- echo "<font color='red'>" . $message . "</font>";
- echo "<br />";
- } else {
- echo "<fieldset>";
- echo "<legend>错误信息提示:</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'>错误号:12142</font>";
- echo "</div><br />";
- echo "错误原因:" . 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 . "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;
-
- $file = "error";
-
-
- if (!file_exists($file)) {
- if (!mkdir($file, 0777)) {
-
- die("upload files directory does not exist and creation failed");
- }
- }
-
-
- if (!file_exists($file_path)) {
-
-
- fopen($file_path, "w+");
-
-
- if (is_writable($file_path)) {
-
- if (!$handle = fopen($file_path, 'a')) {
- echo "不能打开文件 $filename";
- exit;
- }
-
-
- if (!fwrite($handle, $error_content)) {
- echo "不能写入到文件 $filename";
- exit;
- }
-
-
-
- echo "——错误记录被保存!";
-
-
- fclose($handle);
- } else {
- echo "文件 $filename 不可写";
- }
-
- } else {
-
- if (is_writable($file_path)) {
-
- if (!$handle = fopen($file_path, 'a')) {
- echo "不能打开文件 $filename";
- exit;
- }
-
-
- if (!fwrite($handle, $error_content)) {
- echo "不能写入到文件 $filename";
- exit;
- }
-
-
- echo "——错误记录被保存!";
-
-
- fclose($handle);
- } else {
- echo "文件 $filename 不可写";
- }
- }
-
- }
- echo "<br />";
- if ($this->is_error) {
- exit;
- }
- }
- echo "</div>";
- echo "</fieldset>";
-
- echo "<br />";
- }
-
-
- 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) {
-
- $this->query("select * from $table_name");
- echo "<br />";
- echo "字段数:" . $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 />";
- }
-
-
- public function mysql_server($num = '') {
- switch ($num) {
- case 1 :
- return mysql_get_server_info();
- break;
-
- case 2 :
- return mysql_get_host_info();
- break;
-
- case 3 :
- return mysql_get_client_info();
- break;
-
- case 4 :
- return mysql_get_proto_info();
- break;
-
- default :
- return mysql_get_client_info();
- }
- }
-
-
- public function __destruct() {
- if (!emptyempty ($this->result)) {
- $this->free();
- }
- mysql_close($this->conn);
- }
-
-
- 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|'|
-
-
-
-
-
-
-
-
- i", "1", $_server['http_referer']) !== preg_replace("/([^:]+).*/", "1", $_server['http_host'])) {
- header("location: http://www.phpfensi.com");
- exit();
- }
- }
-
- }
|
|
|
|