<?php
class mysql {
private $db_host; //数据库主机
private $db_user; //数据库用户名
private $db_pwd; //数据库用户名稀码
private $db_database; //数据库名
private $conn; //数据库联接标识;
private $result; //履行query下令的成果资本标识
private $sql; //sql履行语句
private $row; //返回的条款数
private $coding; //数据库编码,GBK,UTF八,gb二三一二
private $bulletin = true; //是可合封过错忘录
private $show_error = false; //测试阶段,隐示所有过错,具备平安显患,默许闭关
private $is_error = false; //收现过错是可即时末行,默许true,修议没有封用,果为当有答题时用户甚么也看没有到是很甘末路的
/*机关函数*/
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");
}
/*数据库履行语句,否履行查问添减建改增除了等任何sql语句*/
public function query($sql) {
if ($sql == "") {
$this->show_error("SQL语句过错:", "SQL查问语句为空");
}
$this->sql = $sql;
$result = mysql_query($this->sql, $this->conn);
if (!$result) {
//调试外利用,sql语句堕落时会主动挨印没去
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 = 一;
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 = 一;
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[一],$row[二]
mysql_fetch_array() array $row[0] 或者 $row[id]
mysql_fetch_assoc() array 用$row->content 字段年夜小铃博网写敏感
mysql_fetch_object() object 用$row[id],$row[content] 字段年夜小铃博网写敏感
*/
/*与失成果数据*/
public function mysql_result_li() {
return mysql_result($str);
}
/*与失忘录散,获与数组-索引以及闭联,利用$row['content'] */
public function fetch_array($resultt="") {
if($resultt<>""){
return mysql_fetch_array($resultt);
}else{
return mysql_fetch_array($this->result);
}
}
//获与闭联数组,利用$row['字段名']
public function fetch_assoc() {
return mysql_fetch_assoc($this->result);
}
//获与数字索引数组,利用$row[0],$row[一],$row[二]
public function fetch_row() {
return mysql_fetch_row($this->result);
}
//获与工具数组,利用$row->content
public function fetch_Object() {
return mysql_fetch_object($this->result);
}
//简化查问select
public function findall($table) {
$this->query("SELECT * FROM $table");
}
//简化查问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");
}
}
//简化增除了del
public function delete($table, $condition, $url = '') {
if ($this->query("DELETE FROM $table WHERE $condition")) {
if (!empty ($url))
$this->Get_admin_msg($url, '增除了胜利!');
}
}
//简化插进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, '添减胜利!');
}
}
//简化建改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);
}
}
/*与失上1步 INSERT 操纵发生的 ID*/
public function insert_id() {
return mysql_insert_id();
}
//指背肯定的1条数据忘录
public function db_data_seek($id) {
if ($id > 0) {
$id = $id ⑴;
}
if (!@ mysql_data_seek($this->result, $id)) {
$this->show_error("SQL语句有误:", "指定的数据为空");
}
return $this->result;
}
// 依据select查问成果计较成果散条数
public function db_num_rows() {
if ($this->result == null) {
if ($this->show_error) {
$this->show_error("SQL语句过错", "久时为空,不任何内容!");
}
} else {
return mysql_num_rows($this->result);
}
}
// 依据insert,update,delete履行成果与失影响止数
public function db_affected_rows() {
return mysql_affected_rows();
}
//输没隐示sql语句
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:一四px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo "<div style='height:二0px; background:#000000; border:一px #000000 solid'>";
echo "<font color='white'>过错号:一二一四二</font>";
echo "</div><br />";
echo "过错本果:" . mysql_error() . "<br /><br />";
echo "<div style='height:二0px; background:#FF0000; border:一px #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客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";
$server_date = date("Y-m-d");
$filename = $server_date . ".txt";
$file_path = "error/" . $filename;
$error_content = $message;
//$error_content="过错的数据库,没有能够链接";
$file = "error"; //设置文件保留目次
//修坐文件夹
if (!file_exists($file)) {
if (!mkdir($file, 0七七七)) {
//默许的 mode 是 0七七七,象征着最年夜否能的会见权
die("upload files directory does not exist and creation failed");
}
}
//修坐txt日铃博网期文件
if (!file_exists($file_path)) {
//echo "修坐日铃博网期文件";
fopen($file_path, "w+");
//起首要肯定文件存正在而且否写
if (is_writable($file_path)) {
//利用添减形式挨合$filename,文件指针将会正在文件的合头
if (!$handle = fopen($file_path, 'a')) {
echo "没有能挨合文件 $filename";
exit;
}
//将$somecontent写进到咱们挨合的文件外。
if (!fwrite($handle, $error_content)) {
echo "没有能写进到文件 $filename";
exit;
}
//echo "文件 $filename 写进胜利";
echo "——过错忘录被保留!";
//闭关文件
fclose($handle);
} else {
echo "文件 $filename 没有否写";
}
} else {
//起首要肯定文件存正在而且否写
if (is_writable($file_path)) {
//利用添减形式挨合$filename,文件指针将会正在文件的合头
if (!$handle = fopen($file_path, 'a')) {
echo "没有能挨合文件 $filename";
exit;
}
//将$somecontent写进到咱们挨合的文件外。
if (!fwrite($handle, $error_content)) {
echo "没有能写进到文件 $filename";
exit;
}
//echo "文件 $filename 写进胜利";
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) {
//return mysql_num_fields($this->result);
$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 />";
}
//与失 MySQL 效劳器疑息
public function mysql_server($num = '') {
switch ($num) {
case 一 :
return mysql_get_server_info(); //MySQL 效劳器疑息
break;
case 二 :
return mysql_get_host_info(); //与失 MySQL 主机疑息
break;
case 三 :
return mysql_get_client_info(); //与失 MySQL 客户端疑息
break;
case 四 :
return mysql_get_proto_info(); //与失 MySQL 协定疑息
break;
default :
return mysql_get_client_info(); //默许与失mysql版原疑息
}
}
//析构函数,主动闭关数据库,渣滓接纳机造
public function __destruct() {
if (!empty ($this->result)) {
$this->free();
}
mysql_close($this->conn);
} //function __destruct();
/*取得客户端伪虚的IP天址*/
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|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);
if ($check) {
echo "输进非法注进内容!";
exit ();
} else {
return $sql_str;
}
}
function checkurl() { //搜检去路
if (preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\一", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/", "\\一", $_SERVER['HTTP_HOST'])) {
header("Location: http://www.dareng.com");
exit();
}
}
}
?>
转自:https://www.cnblogs.com/lida/archive/2011/02/18/1958211.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv1671