


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、功能强大的 MySQL 数据库操作类主要功能有: MySQL 常用操作、分页、导入与导出 SQL 文件、获取 IP 、 MySQL 错误提示等,相关的图 片、css 和 js 都已经放在附件里面了,自己下载研究吧!PHP 代码:<?phpheader('Content-Type:text/html; charset=utf-8');basename($_SERVER'PHP_SELF')='mysql.inc.php'&&header('Location:http:/'.$_SERVER'HTTP_H
2、 OST'); / 禁止直接访问本页/* 【文件名】 : mysql.inc.php【作 用】 : mysql 数据库操作类【作 者】 : Riyan【版 本】 : version 2.0【修改日期】 : 2010/02/11*/ class mysqlprivate $host;/ 数据库主机private $user;/ 数据库用户名private $pass;/数据库密码private $data;/ 数据库名private $conn;/数据库连接标识private $sql;/ sql语句private $code;/数据库编码, GBK,UTF8,GB2312private
3、 $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 数据库主机地址 (必填 )$
4、user数据库用户名 (必填 )$pass数据库密码 (必填 )$data数据库名 (必填 )$conn数据库连接标识 (必填 )$code数据库编码 (必填 )返回值:无实 例:无*/public function _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-&g
5、t;code=$code; $this->connect();public function _get($name)return $this->$name;public function _set($name,$value)$this->$name=$value;/ 数据库连接private function connect()/ 永if ($this->conn='pconn') $this->conn=mysql_pconnect($this->host,$this->user,$this->pass); 久链接else $t
6、his->conn=mysql_connect($this->host,$this->user,$this->pass); / 临时链接 if (!$this->conn) $this->show_error(' 无法连接服务器 ');$this->select_db($this->data);$this->query('SET NAMES '.$this->code);$this->query("SET CHARACTER_SET_CLIENT='$this->code&
7、#39;");$this->query("SET CHARACTER_SET_RESULTS='$this->code'");/ 数据库选择public function select_db($data) $result=mysql_select_db($data,$this->conn);if (!$result) $this->show_error(' 无法连接数据库 '.$data); return $result;*- 函数名: get_info($num)- 作 用:取得 MySQL 服务器信息-
8、参 数: $num 信息值 (选填 )- 返回值:字符串- 实 例:无* public function get_info($num)switch ($num)case 1:return mysql_get_server_info(); / break;case 2:return mysql_get_host_info(); break;case 3:return mysql_get_proto_info(); / break;default:return mysql_get_client_info(); /取得 MySQL 服务器信息/ 取得 MySQL 主机信息取得 MySQL 协议信息取
9、得 MySQL 客户端信息/*- 函数名: query($sql)- 作 用:数据库执行语句,可执行查询添加修改删除等任何 sql 语句- 参 数: $sql sql 语句 (必填 )- 返回值:布尔- 实 例:无 */ public function query($sql)if (empty($sql) $this->show_error('SQL语句为空 ');$this->sql=preg_replace('/ 2,/',' ',trim($sql);$this->result=mysql_query($this->
10、sql,$this->conn);if (!$this->result) $this->show_error('SQL语句有误 ',true);return $this->result;/*- 函数名: create_db($data)- 作 用:创建添加新的数据库- 参 数: $data 数据库名称 (必填 )- 返回值:字符串- 实 例:无*/public function create_database($data='')$this->query("CREATE DATABASE $data");/ 查询服
11、务器所有数据库public function show_database()$this->query('SHOW DATABASES');$db=array();while ($row=$this->fetch_array() $db=$row'Database' return $db;/ 查询数据库下所有的表public function show_tables($data='')if (!empty($data) $db=' FROM '.$data;$this->query('SHOW TABLES
12、'.$data);$tables=array();while ($row=$this->fetch_row() $tables=$row0; return $tables;/*- 函数名: copy_tables($tb1,$tb2,$where)- 作 用:复制表- 参 数: $tb1 新表名 (必填 )$tb2 待复制表的表名 ( 必填 )$Condition 复制条件 ( 选填 )- 返回值:布尔- 实 例:无*/* INTO $tb1FROMpublic function copy_tables($tb1,$tb2,$Condition='')$this-
13、>query("SELECT $tb2 $Condition");/*- 函数名: Get($Table,$Fileds,$Condition,$Rows)- 作 用:查询数据- 参 数: $Table 表名(必填 )$Fileds 字段名,默认为所有 (选填 )$Condition 查询条件 ( 选填 )$Rows 待查询记录条数,为 0 表示不限制 ( 选填 )- 返回值:布尔- 实 例: $DB->Get('mydb','user,password','order by id desc',10)public
14、function Get($Table,$Fileds='*',$Condition='',$Rows=0)if (!$Fileds) $Fileds='*'if ($Rows>0) $Condition.=" LIMIT 0,$Rows"$sql="SELECT $Fileds FROM $Table $Condition"return $this->query($sql);/ 只查询一条记录public function GetRs($Table,$Fileds='*',$C
15、ondition='')if (!$Fileds) $Fileds='*'$this->query("SELECT $Fileds FROM $Table $Condition LIMIT 0,1"); return $this->fetch_array();/*- 函数名: Add($Table,$Data)- 作 用:添加数据- 参 数: $Table 表名(必填 )$Data 待添加数据 ,可以为数组 (必填 )数组类型- 返回值:布尔- 实 例: $DB->Add('mydb',array('
16、user'=>'admin','password'=>'123456','age'=>'18')$DB->Add('mydb','user=admin,password=123456,age=18') 字符串类型*/public function Add($Table,$Data)if (!is_array($Data)$arr=explode(',',$Data);$Data=array();foreach ($arr as $val
17、)list($key,$val)=explode('=',$val);if (!$val) $val=''$Data$key=$val;$Fileds=''.implode(',',array_keys($Data).''$Value="'".implode("','",array_values($Data)."'"return $this->query("INSERT INTO $Table ($Filed
18、s) VALUES ($Value)");/*- 函数名: Set($Table,$Data,$Condition,$unQuot)- 作 用:更改数据- 参 数: $Table 表名(必填 )$Data 待更改数据 ,可以为数组 (必填 )$Condition 更改条件 ( 选填 )$unQuot 不需要加引号的字段, 用于字段的加减运算等情况, 多个字段用 ,分隔或者写入一个数组(选填)数组类型- 返回值:布尔- 实 例: $DB->Set('mydb',array('user'=>'admin','passwo
19、rd'=>'123456','WHERE id=1')$DB->Set('mydb',"user='admin',password='123456'",'WHERE id=1') 字符串类型*/public function Set($Table,$Data,$Condition='',$unQuot='')if (is_array($Data)if (!is_array($unQuot) $unQuot=explode(
20、9;,',$unQuot);foreach ($Data as $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)- 作 用:删除数据- 参 数:
21、$Table 表名(必填 )$Condition 删除条件 ( 选填 )- 返回值:布尔- 实 例: $DB->Del('mydb','id=1')*/FROMpublic function Del($Table,$Condition='')return $this->query("DELETE$Table".($Condition?" WHERE $Condition":'');/ 取得结果数据public function result($result='')
22、if (empty($result) $result=$this->result;if ($result=null) $this->show_error(' 未获取到查询结果 ',true);return mysql_result($result);/*- 函数名: fetch_array($Table,$Condition)- 作 用:根据从结果集取得的行生成关联数组- 参 数: $result 结果集 (选填 )BOTH( 选填 )$type 数组类型,可以接受以下值: MYSQL_ASSOC , MYSQL_NUM 和 MYSQL- 返回值:布尔- 实 例:
23、$DB->Del('mydb','id=1')*/public function fetch_array($result='',$type=MYSQL_BOTH)if (empty($result) $result=$this->result;if (!$result) $this->show_error(' 未获取到查询结果 ',true); return mysql_fetch_array($result,$type);/ 获取关联数组 ,使用$row'字段名 'public function
24、 fetch_assoc($result='')if (empty($result) $result=$this->result;if (!$result) $this->show_error(' 未获取到查询结果 ',true); return mysql_fetch_assoc($result);/ 获取数字索引数组 ,使用 $row0,$row1,$row2public function fetch_row($result='')if (empty($result) $result=$this->result;if (!$
25、result) $this->show_error(' 未获取到查询结果 ',true); return mysql_fetch_row($result);/ 获取对象数组 ,使用 $row->contentpublic function fetch_obj($result='')if (empty($result) $result=$this->result;if (!$result) $this->show_error(' 未获取到查询结果 ',true); return mysql_fetch_object($res
26、ult);/ 取得上一步 INSERT 操作产生的 IDpublic function insert_id()return mysql_insert_id();/ 指向确定的一条数据记录public function data_seek($id)指定的数据为空 ');if ($id>0) $id=$id-1;if (!mysql_data_seek($this->result,$id) $this->show_error(' return $this->result;/* 函数名: num_fields($result)作 用:查询字段数量参 数: $T
27、able 数据库表名 (必填 )返回值:字符串实 例: $DB->num_fields("mydb")public function num_fields($result='')if (empty($result) $result=$this->result;if (!$result) $this->show_error(' 未获取到查询结果 ',true); return mysql_num_fields($result);/ 根据 select 查询结果计算结果集条数public function num_rows($r
28、esult='') if (empty($result) $result=$this->result;$rows=mysql_num_rows($result);if ($result=null)$rows=0;$this->show_error(' 未获取到查询结果 ',true);return $rows>0?$rows:0;/ 根据 insert,update,delete 执行结果取得影响行数public function affected_rows()return mysql_affected_rows();/ 获取地址栏参数publ
29、ic function getQuery($unset='') /$unset 表 示 不 需 要 获 取 的 参 数 , 多 个 参 数 请 用 , 分 隔 ( 例 如:getQuery('page,sort')if (!empty($unset)$arr=explode(',',$unset);foreach ($arr as $val) unset($_GET$val);foreach ($_GET as $key=>$val) $list=$key.'='.urlencode($val);return is_arra
30、y($list)?implode('&',$list):''/*函数名: getPage($Table,$Fileds,$Condition,$pageSize)作 用:获取分页信息参 数: $Table 表名 (必填 )$Fileds 字段名,默认所有字段 (选填 )$Condition 查询条件 ( 选填 )$pageSize 每页显示记录条数,默认 10 条 (选填 )返回值:字符串实 例:无*/public function getPage($Table,$Fileds='*',$Condition='',$pag
31、eSize=10)if (intval($pageSize)>0)$this->pageSize=intval($pageSize);if (isset($_GET'page') && intval($_GET'page')$this->pageNo=intval($_GET'page');if (empty($Fileds)$Fileds='*'$sql="SELECT * FROM $Table $Condition"$this->query($sql); $this
32、->rsAll=$this->num_rows();if ($this->rsAll>0) $this->pageAll=ceil($this->rsAll/$this->pageSize);if ($this->pageNo<1)$this->pageNo=1;if ($this->pageNo>$this->pageAll)$this->pageNo=$this->pageAll; $sql="SELECT $Fileds FROM $Table $Condition".$this
33、->limit(true); $this->query($sql);return $this->rsAll;/ 构造分页 limit 语句,和 getPage() 函数搭配使用public function limit($str=false) $n=($this->pageNo-1)*$this->pageSize;return $str?' LIMIT '.$n.','.$this->pageSize:$n;/ 显示分页,必须和 getPage() 函数搭配使用 public function showPage($numbe
34、r=true)$pageBar=''if ($this->pageAll>1)$pageBar.='<ul class="page">'.chr(10); $url=$this->getQuery('page');$url=empty($url)?'?page=':'?'.$url.'&page='if ($this->pageNo>1)$pageBar.='<li><a href="'
35、.$url.'1"> 首页 </a></li>'.chr(10);$pageBar.='<li><a href="'.$url.($this->pageNo-1).'"> 上页 </a></li>'.chr(10); else$pageBar.='<li class="stop"><span>首页 </span></li>'.chr(10);$page
36、Bar.='<li class="stop"><span>上页 </span></li>'.chr(10);if ($number)$arr=array();if ($this->pageAll<6)for ($i=0;$i<$this->pageAll;$i+) $arr=$i+1;elseif ($this->pageNo<3) $arr=array(1,2,3,4,5);elseif ($this->pageNo<=$this->pageAll&
37、;&$this->pageNo>($this->pageAll-3) for ($i=1;$i<6;$i+) $arr=$this->pageAll-5+$i;elsefor ($i=1;$i<6;$i+) $arr=$this->pageNo-3+$i;foreach ($arr as $val)if ($val=$this->pageNo) $pageBar.='<li class="curr"><span>'.$val.'</span></li&g
38、t;'.chr(10);else $pageBar.='<li><a href="'.$url.$val.'">'.$val.'</a></li>'.chr(10);if ($this->pageNo<$this->pageAll)$pageBar.='<li><a href="'.$url.($this->pageNo+1).'"> 下页 </a>'.chr(
39、10);$pageBar.='<li><a href="'.$url.$this->pageAll.'">尾页 </a></li>'.chr(10);else$pageBar.='<li class="stop"><span>下页 </span></li>'.chr(10);$pageBar.='<li class="stop"><span>尾页 <
40、/span></li>'.chr(10);$pageBar.='<li class="stop"><span>'$pageBar.=" 页次 :$this->pageNo/$this->pageAll $this->pageSize 条 / 页 总记录 :$this->rsAll 转到 :"$pageBar.="<input id="page" value="$this->pageNo" type=&qu
41、ot;text" onblur="goPage('$url',$this->pageAll);" />"$pageBar.='</span></li></ul>'.chr(10);echo $pageBar;/ 获得客户端真实的 IP 地址public function getip()if ($_SERVER'HTTP_X_FORWARDED_FOR') return $_SERVER'HTTP_X_FORWARDED_FOR'elseif (
42、$_SERVER'HTTP_CLIENT_IP') return $_SERVER'HTTP_CLIENT_IP'elseif ($_SERVER'REMOTE_ADDR') return $_SERVER'REMOTE_ADDR'elseif (getenv('HTTP_X_FORWARDED_FOR') return getenv('HTTP_X_FORWARDED_FOR');elseif (getenv('HTTP_CLIENT_IP') return getenv('
43、;HTTP_CLIENT_IP');elseif (getenv('REMOTE_ADDR') return getenv('REMOTE_ADDR');else return ''/*- 函数名: show_error($message,$sql)- 作 用:输出显示错误信息- 参 数: $msg 错误信息 (必填 )$sql 显示错误的 SQL 语句,在 SQL 语句错误时使用 (选填 ) - 返回值:字符串- 实 例:无 */ public function show_error($msg='',$sql=false
44、)$err=''.mysql_errno().''.mysql_error();if ($sql) $sql='SQL 语句: '.$this->sql; if ($this->errLog)$dirs='error/' / 设置错误日志保存目录 $fileName=date('Y-m-d').'.log'$filePath=$dirs.$fileName;if (!is_dir($dirs) $dirs=explode('/',$dirs); $temp='
45、39; foreach($dirs as $dir)$temp.=$dir.'/'if (!is_dir($temp)');客户 端 IP:'.$fileName:' 写mkdir($temp,0777) or die('_无法建立目录 '.$temp.' ,自动取消记录错误信息$filePath=$temp.$fileName;$text=" 错 误 事 件 : ".$msg."rn 错 误 原 因 : ".$err."rn".($sql?$sql."rn&q
46、uot;:'')." ".$this->getip()."rn 记录时间: ".date('Y-m-d H:i:s')."rnrn"$log=' 错误日志: _'.(error_log($text,3,$filePath)?' 此错误信息已被自动记录到日志 入错误信息到日志失败 ');if ($this->showErr)echo '<fieldset class="errlog"><legend> 错误信息
47、提示 </legend><label class="tip"> 错误事件: '.$err.'</label><label class="msg"> 错误原因: '.$msg.'</label><label class="sql">'.$sql.'</label><label class="log">'.$log.'</label></fie
48、ldset>'exit();/*- 函数名: drop($table)- 作 用:删除表 (请慎用 , 无法恢复 )- 参 数: $table 要删除的表名,默认为所有 (选填 )- 返回值:无- 实 例: $DB->drop('mydb') */ public function drop($table)if ($table)$this->query("DROP TABLE IF EXISTS $table");else$rst=$this->query('SHOW TABLES');while ($row=$
49、this->fetch_array() $this->query("DROP TABLE IF EXISTS $row0");/*- 函数名: makeSql($table)- 作 用:从数据表读取信息并生成 SQL 语句- 参 数: $table 待读取的表名 (必填 )- 返回值:字符串- 实 例:无*/ public function makeSql($table)$result=$this->query("SHOW CREATE TABLE $table");$row=$this->fetch_row($result);$
50、sqlStr=''if ($row)$sqlStr.="- rn"$sqlStr.="- Table structure for $tablern"$sqlStr.="- rn"$sqlStr.="DROP TABLE IF EXISTS $table;rn$row1;rn" $this->Get($table);$fields=$this->num_fields();if ($this->num_rows()>0)$sqlStr.="rn"$sqlStr
51、.="- rn"$sqlStr.="- Records of $tablern"$sqlStr.="- rn"while ($row=$this->fetch_row()$comma=''$sqlStr.="INSERT INTO $table VALUES (" for($i=0;$i<$fields;$i+)$sqlStr.=$comma."'".mysql_escape_string($row$i)."'" $comma=
52、39;,'$sqlStr.=");rn"$sqlStr.="rn"return $sqlStr;/*- 函数名: readSql($filePath)- 作 用:读取 SQL 文件并过滤注释- 参 数: $filePath SQL 文件路径 (必填)- 返回值:字符串 /布尔 /数组- 实 例:无*/public function readSql($filePath) if (!file_exists($filePath) return false;$sql=file_get_contents($filePath);if (empty($sql)
53、 return ''$sql=preg_replace('/(/*(.*)*/)/s','',$sql); / 过滤批量注释$sql=preg_replace('/(-.*)|fnrtv*/','',$sql); /过滤单行注释与回车换行符$sql=preg_replace('/ 2,/',' ',$sql); / 将两个以上的连续空格替换为一个,可以省略这一步 $arr=explode('',$sql);$sql=array();foreach ($arr as $str)$str=trim($str);if (!empty($str) $sql=$str;return $sql;/*- 函数名: saveSql($sqlPath,$table)(选填)- 作 用:将当前数据库信息保存为 SQL
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 模块建房合同范本
- 语文课程实践技能知到课后答案智慧树章节测试答案2025年春广州大学
- 模板内容展示
- 2025标准合同终止协议书2
- 高一英语学案:预习导航Themeparks-SectionⅢ
- 2024年山东济南轨道交通酒店管理有限公司招聘真题
- 2024年平凉市灵台县公安局招聘警务辅助人员真题
- 2024年六盘水市直事业单位遴选工作人员真题
- 2024年安龙县中等职业学校专任教师招聘真题
- 2024年承德丰宁满族自治县招聘社区工作者真题
- 《HSK标准教程3》第5课课件
- HSK标准教程4上第1课课件
- 民俗学概论 第一章 概述课件
- 养老机构行政值班查房记录表格
- 干粉灭火器点检记录表(样表)
- 伍光和自然地理学4版知识点总结课后答案
- 滇10J6-1住宅厨房、卫生间烟气道及管道井构造图集
- 110kv变电站电气主接线设计资料全
- 华中科技大学版五年级信息技术教案
- 围术期患者转运专家共识
- 铁路货物运价规则铁运[2005]46号
评论
0/150
提交评论