<?php
class mysql {
/*+++++++++++++++数据库访问类++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
//=================================================
//连接数据
private $Host = "localhost"; //服务器地址
private $Database = "sansu"; //数据库名称
private $User = "root"; //用户名
private $Password = "czq339403229"; //用户密码
private $Char_Set = "utf8"; //设置数据库字符集
//结果数据
public $Link_ID = 0; //数据库连接
public $Query_ID = 0; //查询结果
public $Row_Result = array(); //结果节组成的数组
public $Field_Result = array(); //结果集字段组成的数组
// public $Affected_Rows=null; //影响的行数
// public $Rows = null; //结果集行数
// public $Fields = null; //结果集字段数
// public $Row_Position = null; //记录指针位置索引
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// _______________________________________________________________
/*
+++++++++++构造函数++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
function __construct() {
$this->connect();
}
/*
+++++++++++++++++++++++++析构函数+++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
function __destruct() {
// mysql_free_result($this->Query_ID);
mysql_close($this->Link_ID);
}
/*
+++++++++++++++++++连接服务器,选择数据库+++++++++++++++++++++++*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
private function connect() {
if(0 == $this->Link_ID){
$this->Link_ID = @mysql_connect($this->Host, $this->User ,$this->Password);
if(!$this->Link_ID){
$this->halt("连接数据库服务端失败");
}
if(!mysql_select_db($this->Database)){
$this->halt("不能连接指定的数据库:".$this->Database);
}
if(!mysql_query("set names ".$this->Char_Set)){
$this->halt("不能设定指定的字符格式:".$this->Char_Set);
}
}
return $this->Link_ID;
}
/*
++++++++++++++++++++释放内存+++++++++++++++++++++++++++++++++++++++++
*/
private function free() {
if(mysql_free_result($this->Query_ID))
unset($this->Row_Result);
$this->Query_ID = 0;
}
/*
++++++++++++++++++++++执行查询+++++++++++++++++++++++++++++++++++++++++++++
*/
public function query($query_string) {
if($this->Query_ID){
$this->free();
}
if($this->Link_ID == 0){
$this->connect();
}
$this->Query_ID = @mysql_query($query_string);
if(!$this->Query_ID){
$this->halt("SQL查询语句出错:".$query_string);
}
return $this->Query_ID;
}
/*
++++++++++++++++++++++++*将结果集指针指向指定行++++++++++++++++++++++++++++
*/
public function seek($pos) {
if(@mysql_data_seek($this->Query_ID , $pos)){
//$this->Row_Position = $pos;
return true;
}
else{
$this->halt("定位结果发生错误");
return false;
}
}
/*
++++++++++++++++++++++++++++返回结果集组成的数组+++++++++++++++++++++++++++++++
*/
public function get_array($query_string) {
$Rows = $this->get_rows($query_string);
for($i=0; $i<$Rows; $i++){
$this->Row_Result[$i] = @mysql_fetch_array($this->Query_ID);
}
return $this->Row_Result;
}
/*
+++++++++++++++++++++++++++++返回一条记录++++++++++++++++++++++++++++++++
*/
public function get_one($query_string) {
$this->query($query_string);
$array = @mysql_fetch_array($this->Query_ID);
return $array;
}
/*
++++++++++++++++++++++++++++++简单的mysql_fetch_array查询++++++++++++++++++++++++++++++++++++++++
*/
public function my_fetch_array() {
$array = @mysql_fetch_array($this->Query_ID);
return $array;
}
/*
++++++++++++++++++++++++++返回结果集字段组成的数组+++++++++++++++++++++++++++
*/
public function get_field_array($query_string) {
$Fields = $this->get_fields($query_string);
for($i=0;$i<$Fields;$i++){
$obj = @mysql_fetch_field($this->Query_ID);
$this->Field_Result[$i] = $obj->name;
}
return $this->Field_Result;
}
/*
+++++++++++++++++++++返回影响记录数+++++++++++++++++++++++++++++++++++
*/
public function get_affected_rows() {
// $this->Affected_Rows = mysql_affected_rows($this->Link_ID);
$Affected_Rows = mysql_affected_rows($this->Link_ID);
return $Affected_Rows;
}
/*++++++++++++++++++++返回结果集中记录行数+++++++++++++++++++++++++++++*/
public function get_rows($query_string) {
$this->query($query_string);
$Rows = mysql_num_rows($this->Query_ID);
return $Rows;
}
/*+++++++++++++++++++++++返回结果集中字段个++++++++++++++++++++++++++++*/
public function get_fields($query_string) {
$this->query($query_string);
$Fields = mysql_num_fields($this->Query_ID);
return $Fields;
}
/*
+++++++++++++++++++++++++++++++登陆判断函数++++++++++++++++++++++++++++++++++++++++
__________________________________________________________________________________
@param string $query_string query查询语句
@param string $pass 密码
@param boolean $flag 设置session(true)或者cookie(false)
@param string $url 登陆成功转向页面,默认为:index.php
___________________________________________________________________________________
查询语句只能按查询按id、用户名、密码顺序查询
*/
public function check_login($query_string, $password, $flag=true, $url="index.php") {
$key = $this->get_field_array($query_string);
$info = $this->my_fetch_array();
if(empty($info)) {
header("Content-type: text/html;charset=utf-8");
echo '<script>alert("用户名或密码错误");history.go(-1);</script>';
exit;
}
else if($info[$key['2']]!=md5($password)) {
header("Content-type: text/html;charset=utf-8");
echo '<script>alert("用户名或密码错误");history.go(-1);</script>';
exit;
}
else if($flag) {
$_SESSION['information'] = $info[$key[0]]."#".$info[$key[1]]."#".md5($password);
}
else {
$information = $info[$key[0]]."#".$info[$key[1]]."#".md5($password);
setcookie("information","",time()-900);
setcookie("information",$information,time()+900); //cookie有效期为900秒即15分钟
}
header("Content-type: text/html;charset=utf-8");
echo '<script>alert("登陆成功");location="'.$url.'"</script>';
}
/**
++++++++++获取客户端IP地址+++++++++
____________________________________________________
返回IP地址
______________________________________________________
**/
private function get_client_ip(){
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);
}
/**
++++++++++++++++++++++记录IP+++++++++++++++++++++++++
**/
public function insert_ip(){
$ip = $this->get_client_ip();
$sql = "insert into `ip_address`(`ID`,`ADDRESS`,`DETAIL_TIME`) values('','$ip',now())";
$this->query($sql);
}
/*+++++++++++++++++++++打印错误信息++++++++++++++++++++++++++++++++++*/
private function halt($msg) {
$error = mysql_error();
printf("<br /><b>数据库发生错误:</b> %s<br />/n", $msg);
printf("<b>MYSQL返回错误信息: </b> %s<br />", $error);
die("终止脚本");
}
}
/*$sql = "select `id`,`content`,`title` from `think_demo` where id='1'";
$mysql = new mysql();
$fields = $mysql->check_login($sql,"a");
echo $_SESSION[information];*/
?>