PHP功能强大的MySQL数据库操作类.docx
《PHP功能强大的MySQL数据库操作类.docx》由会员分享,可在线阅读,更多相关《PHP功能强大的MySQL数据库操作类.docx(19页珍藏版)》请在冰豆网上搜索。
PHP功能强大的MySQL数据库操作类
PHP功能强大的MySQL数据库操作类
主要功能有:
MySQL常用操作、分页、导入与导出SQL文件、获取IP、MySQL错误提示等……
PHP代码:
php
header('Content-Type:
text/html;charset=utf-8');
basename($_SERVER['PHP_SELF'])=='mysql.inc.php'&&header('Location:
http:
//'.$_SERVER['HTTP_HOST']);//禁止直接访问本页
/**
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
【文件名】:
mysql.inc.php
【作 用】:
mysql数据库操作类
【作 者】:
Riyan
【版 本】:
version2.0
【修改日期】:
2010/02/11
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
**/
classmysql{
private$host; //数据库主机
private$user; //数据库用户名
private$pass; //数据库密码
private$data; //数据库名
private$conn; //数据库连接标识
private$sql; //sql语句
private$code; //数据库编码,GBK,UTF8,GB2312
private$result; //执行query命令的结果数据集
private$errLog=true; //是否开启错误日志,默认开启
private$showErr=true;//显示所有错误,具有安全隐患,默认开启
private$pageNo=1; //当前页
private$pageAll=1; //总页数
private$rsAll=0; //总记录
private$pageSize=10; //每页显示记录条数
/******************************************************************
--函数名:
__construct($host,$user,$pass,$data,$code,$conn)
--作 用:
构造函数
--参 数:
$host数据库主机地址(必填)
$user数据库用户名(必填)
$pass数据库密码(必填)
$data数据库名(必填)
$conn数据库连接标识(必填)
$code数据库编码(必填)
--返回值:
无
--实 例:
无
*******************************************************************/
publicfunction__construct($host,$user,$pass,$data,$code='utf8',$conn='conn'){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->data=$data;
$this->conn=$conn;
$this->code=$code;
$this->connect();
}
publicfunction__get($name){return$this->$name;}
publicfunction__set($name,$value){$this->$name=$value;}
//数据库连接
privatefunctionconnect(){
if($this->conn=='pconn')$this->conn=mysql_pconnect($this->host,$this->user,$this->pass);//永久链接
else$this->conn=mysql_connect($this->host,$this->user,$this->pass);//临时链接
if(!
$this->conn)$this->show_error('无法连接服务器');
$this->select_db($this->data);
$this->query('SETNAMES'.$this->code);
$this->query("SETCHARACTER_SET_CLIENT='{$this->code}'");
$this->query("SETCHARACTER_SET_RESULTS='{$this->code}'");
}
//数据库选择
publicfunctionselect_db($data){
$result=mysql_select_db($data,$this->conn);
if(!
$result)$this->show_error('无法连接数据库'.$data);
return$result;
}
/******************************************************************
--函数名:
get_info($num)
--作 用:
取得MySQL服务器信息
--参 数:
$num信息值(选填)
--返回值:
字符串
--实 例:
无
*******************************************************************/
publicfunctionget_info($num){
switch($num){
case1:
returnmysql_get_server_info();//取得MySQL服务器信息
break;
case2:
returnmysql_get_host_info(); //取得MySQL主机信息
break;
case3:
returnmysql_get_proto_info(); //取得MySQL协议信息
break;
default:
returnmysql_get_client_info();//取得MySQL客户端信息
}
}
/******************************************************************
--函数名:
query($sql)
--作 用:
数据库执行语句,可执行查询添加修改删除等任何sql语句
--参 数:
$sqlsql语句(必填)
--返回值:
布尔
--实 例:
无
*******************************************************************/
publicfunctionquery($sql){
if(empty($sql))$this->show_error('SQL语句为空');
$this->sql=preg_replace('/{2,}/','',trim($sql));
$this->result=mysql_query($this->sql,$this->conn);
if(!
$this->result)$this->show_error('SQL语句有误',true);
return$this->result;
}
/******************************************************************
--函数名:
create_db($data)
--作 用:
创建添加新的数据库
--参 数:
$data数据库名称(必填)
--返回值:
字符串
--实 例:
无
*******************************************************************/
publicfunctioncreate_database($data=''){$this->query("CREATEDATABASE{$data}");}
//查询服务器所有数据库
publicfunctionshow_database(){
$this->query('SHOWDATABASES');
$db=array();
while($row=$this->fetch_array())$db[]=$row['Database'];
return$db;
}
//查询数据库下所有的表
publicfunctionshow_tables($data=''){
if(!
empty($data))$db='FROM'.$data;
$this->query('SHOWTABLES'.$data);
$tables=array();
while($row=$this->fetch_row())$tables[]=$row[0];
return$tables;
}
/******************************************************************
--函数名:
copy_tables($tb1,$tb2,$where)
--作 用:
复制表
--参 数:
$tb1新表名(必填)
$tb2待复制表的表名(必填)
$Condition复制条件(选填)
--返回值:
布尔
--实 例:
无
*******************************************************************/
publicfunctioncopy_tables($tb1,$tb2,$Condition=''){$this->query("SELECT*INTO`{$tb1}`FROM`{$tb2}`{$Condition}");}
/******************************************************************
--函数名:
Get($Table,$Fileds,$Condition,$Rows)
--作 用:
查询数据
--参 数:
$Table表名(必填)
$Fileds字段名,默认为所有(选填)
$Condition查询条件(选填)
$Rows待查询记录条数,为0表示不限制(选填)
--返回值:
布尔
--实 例:
$DB->Get('mydb','user,password','orderbyiddesc',10)
*******************************************************************/
publicfunctionGet($Table,$Fileds='*',$Condition='',$Rows=0){
if(!
$Fileds)$Fileds='*';
if($Rows>0)$Condition.="LIMIT0,{$Rows}";
$sql="SELECT{$Fileds}FROM`{$Table}`{$Condition}";
return$this->query($sql);
}
//只查询一条记录
publicfunctionGetRs($Table,$Fileds='*',$Condition=''){
if(!
$Fileds)$Fileds='*';
$this->query("SELECT{$Fileds}FROM`{$Table}`{$Condition}LIMIT0,1");
return$this->fetch_array();
}
/******************************************************************
--函数名:
Add($Table,$Data)
--作 用:
添加数据
--参 数:
$Table表名(必填)
$Data待添加数据,可以为数组(必填)
--返回值:
布尔
--实 例:
$DB->Add('mydb',array('user'=>'admin','password'=>'123456','age'=>'18')数组类型
$DB->Add('mydb','user=admin,password=123456,age=18')字符串类型
*******************************************************************/
publicfunctionAdd($Table,$Data){
if(!
is_array($Data)){
$arr=explode(',',$Data);
$Data=array();
foreach($arras$val){
list($key,$val)=explode('=',$val);
if(!
$val)$val='';
$Data[$key]=$val;
}
}
$Fileds='`'.implode('`,`',array_keys($Data)).'`';
$Value="'".implode("','",array_values($Data))."'";
return$this->query("INSERTINTO`{$Table}`({$Fileds})VALUES({$Value})");
}
/******************************************************************
--函数名:
Set($Table,$Data,$Condition,$unQuot)
--作 用:
更改数据
--参 数:
$Table表名(必填)
$Data待更改数据,可以为数组(必填)
$Condition更改条件(选填)
$unQuot不需要加引号的字段,用于字段的加减运算等情况,多个字段用,分隔或者写入一个数组(选填)
--返回值:
布尔
--实 例:
$DB->Set('mydb',array('user'=>'admin','password'=>'123456','WHEREid=1')数组类型
$DB->Set('mydb',"user='admin',password='123456'",'WHEREid=1')字符串类型
*******************************************************************/
publicfunctionSet($Table,$Data,$Condition='',$unQuot=''){
if(is_array($Data)){
if(!
is_array($unQuot))$unQuot=explode(',',$unQuot);
foreach($Dataas$key=>$val){
$arr[]=$key.'='.(in_array($key,$unQuot)?
$val:
"'$val'");
}
$Value=implode(',',$arr);
}else$Value=$Data;
return$this->query("UPDATE`{$Table}`SET{$Value}{$Condition}");
}
/******************************************************************
--函数名:
Del($Table,$Condition)
--作 用:
删除数据
--参 数:
$Table表名(必填)
$Condition删除条件(选填)
--返回值:
布尔
--实 例:
$DB->Del('mydb','id=1')
*******************************************************************/
publicfunctionDel($Table,$Condition=''){return$this->query("DELETEFROM`{$Table}`".($Condition?
"WHERE{$Condition}":
''));}
//取得结果数据
publicfunctionresult($result=''){
if(empty($result))$result=$this->result;
if($result==null)$this->show_error('未获取到查询结果',true);
returnmysql_result($result);
}
/******************************************************************
--函数名:
fetch_array($Table,$Condition)
--作 用:
根据从结果集取得的行生成关联数组
--参 数:
$result结果集(选填)
$type数组类型,可以接受以下值:
MYSQL_ASSOC,MYSQL_NUM和MYSQL_BOTH(选填)
--返回值:
布尔
--实 例:
$DB->Del('mydb','id=1')
*******************************************************************/
publicfunctionfetch_array($result='',$type=MYSQL_BOTH){
if(empty($result))$result=$this->result;
if(!
$result)$this->show_error('未获取到查询结果',true);
returnmysql_fetch_array($result,$type);
}
//获取关联数组,使用$row['字段名']
publicfunctionfetch_assoc($result=''){
if(empty($result))$result=$this->result;
if(!
$result)$this->show_error('未获取到查询结果',true);
returnmysql_fetch_assoc($result);
}
//获取数字索引数组,使用$row[0],$row[1],$row[2]
publicfunctionfetch_row($result=''){
if(empty($result))$result=$this->result;
if(!
$result)$this->show_error('未获取到查询结果',true);
returnmysql_fetch_row($result);
}