主要功能有:MySQL常用操作、分页、导入与导出SQL文件、获取IP、MySQL错误提示等,相关的图片、css和js都已经放在附件里面了,自己下载研究吧!
有人问CSS和JS文件的作用,我做个说明吧!
CSS主要是用于错误提示和分页,下面是效果图:
分页

错误提示

JS主要用于分页里面的跳转功能(后面的输入框)
- <?php
- /**
- ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
- 【类 名】: MySql
- 【功 能】: mysql数据库操作类
- 【作 者】: Riyan
- 【日 期】: 2010/5/8
- 【版 本】: version 2.2c
- ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
- **/
- header('Content-Type:text/html; charset=utf-8');
- strtolower(basename($_SERVER['PHP_SELF'])) == 'mysql.inc.php' && header('Location:http://' . $_SERVER['HTTP_HOST']); //禁止直接访问本页
- class MySql{
- private $host; // 数据库主机.
- private $user; // 数据库用户名.
- private $pass; // 数据库密码.
- private $data; // 数据库名.
- private $conn; // 数据库连接标识,0为mysql_connect,1为mysql_pconnect.
- private $sql; // sql语句.
- private $code; // 数据库编码,GBK,UTF8,GB2312.
- public $result; // 执行query命令的结果数据集.
- public $errTip = 'on'; // 是否开启错误提示,具有安全隐患,默认开启,建议仅在本地调试时使用.
- public $errLog = 'on'; // 是否开启错误日志,默认开启,推荐开启.
- public $errDir = 'error'; // 错误日志保存路径,须开启错误日志才有效,可以在类实例化以后自定义.
- public $numRows = 0; // 总记录.
- private $pageAll = 1; // 总页数.
- private $pageNow = 1; // 当前页.
- private $pageSize = 10; // 每页显示记录条数.
- /*------------------------------------------------------------------
- * 函数名: __construct($host, $user, $pass, $data, $code, $conn)
- * 作 用: 构造函数
- * 参 数: $host 数据库主机地址(必填)
- $user 数据库用户名(必填)
- $pass 数据库密码(必填)
- $data 数据库名(必填)
- $code 数据库编码(必填,默认为utf8)
- $conn 数据库连接标识,0(默认)为mysql_connect,1为mysql_pconnect(选填)
- * 返回值: 无
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function __construct($host = 'localhost', $user = 'root', $pass = '', $data = '', $code = 'utf8', $conn = 0) {
- $this->host = $host;
- $this->user = $user;
- $this->pass = $pass;
- $this->data = $data;
- $this->conn = $conn;
- $this->code = $code;
- $this->connect();
- }
- // 访问一个对象并不拥有的属性(私有属性)
- public function __get($property) {
- return $this->$property;
- }
- // 设置一个对象并不拥有的属性(私有属性)
- public function __set($property, $value) {
- $this->$property = $value;
- }
- // 数据库连接
- private function connect() {
- if ($this->conn == 1) {
- $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->showError('无法连接服务器');
- }
- $this->selectDb($this->data);
- $this->query("SET NAMES {$this->code}");
- $this->query("SET CHARACTER_SET_CLIENT={$this->code}");
- $this->query("SET CHARACTER_SET_RESULTS={$this->code}");
- }
- // 数据库选择
- public function selectDb($db = '') {
- if (empty($db)) {
- $this->showError('未设置数据库名称');
- }
- $result = mysql_select_db($db, $this->conn);
- if (!$result) {
- $this->showError('无法连接数据库'.$db);
- }
- return $result;
- }
- /*------------------------------------------------------------------
- * 函数名: getInfo($type)
- * 作 用: 取得 MySQL 服务器信息
- * 参 数: $type 要获取的信息类型(选填)
- * 返回值: 字符串
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function getInfo($type = 0) {
- switch ($type) {
- case 1 : return mysql_get_host_info(); // 取得 MySQL 主机信息
- case 2 : return mysql_get_proto_info(); // 取得 MySQL 协议信息
- case 3 : return mysql_get_server_info(); // 取得 MySQL 服务器信息
- default : return mysql_get_client_info(); // 取得 MySQL 客户端信息
- }
- }
- /*------------------------------------------------------------------
- * 函数名: query($sql)
- * 作 用: 数据库执行语句,可执行查询添加修改删除等任何sql语句
- * 参 数: $sql sql语句(必填)
- * 返回值: 布尔
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function query($sql) {
- $sql = trim($sql);
- if (empty($sql)) {
- $this->showError('SQL语句为空');
- }
- $this->sql = $sql;
- $this->result = mysql_query($this->sql, $this->conn);
- if (!$this->result) {
- $this->showError('SQL语句有误', true);
- }
- return $this->result;
- }
- /*------------------------------------------------------------------
- * 函数名: createDataBase($db)
- * 作 用: 创建添加新的数据库
- * 参 数: $db 数据库名称(必填)
- * 返回值: 字符串
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function createDataBase($db = '') {
- if (empty($db)) {
- $this->showError('未设置待添加的数据库名称');
- }
- $this->query("CREATE DATABASE `{$db}`");
- }
- // 列出当前服务器中的所有数据库
- public function listDataBase() {
- $list = array();
- $this->query('SHOW DATABASES');
- while ($row = $this->fetch()) {
- $list[] = $row['Database'];
- }
- return $list;
- }
- // 列出数据库中的所有表
- public function listTables($db = '') {
- $list = array();
- if (!empty($db)) {
- $db = ' FROM `'.$db.'`';
- }
- $this->query('SHOW TABLES'.$db);
- while ($row = $this->fetch('', 2)) {
- $list[] = $row[0];
- }
- return $list;
- }
-
- // 列出表中的所有字段名
- public function listFields($db = '', $tb = '') {
- if (empty($table)) {
- return false;
- }
- if (empty($db)) {
- $db = $this->data;
- }
- $fields = mysql_list_fields($db, $tb, $this->conn);
- $column = mysql_num_fields($fields);
- $list = array();
- for ($i = 0; $i < $column; $i++) {
- $list[] = mysql_field_name($list, $i);
- }
- return $list;
- }
- /*------------------------------------------------------------------
- * 函数名: copyTable($dstTable, $srcTable, $condition)
- * 作 用: 复制表
- * 参 数: $dstTable 待复制表的表名(必填)
- $srcTable 新表名(必填)
- $condition 复制条件(选填)
- * 返回值: 布尔
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function copyTable($dstTable, $srcTable, $condition = '') {
- return $this->query("SELECT * INTO `{$dstTable}` FROM `{$srcTable}` {$condition}");
- }
- /*------------------------------------------------------------------
- * 函数名: dropTable($table)
- * 作 用: 删除表(请慎用,无法恢复)
- * 参 数: $table 要删除的表名,多个表请用数组,为空表示保存所有表信息(选填)
- * 返回值: 无
- * 实 例: $DB->dropTable('mydb')
- -------------------------------------------------------------------*/
- public function dropTable($table = '') {
- if (!is_array($table)) {
- $table = empty($table) ? $this->listTables($this->data) : array($table);
- }
- foreach ($table as $tb) {
- $this->query("DROP TABLE IF EXISTS `{$tb}`");
- }
- }
- /*------------------------------------------------------------------
- * 函数名: getData($table, $fileds, $condition, $rows)
- * 作 用: 查询数据
- * 参 数: $table 表名(必填)
- $fileds 字段名,默认为所有(选填)
- $condition 查询条件(选填)
- $rows 待查询记录条数,为0表示不限制(选填)
- * 返回值: 布尔
- * 实 例: $DB->getData('mydb','user,password','order by id desc',10)
- -------------------------------------------------------------------*/
- public function getData($table, $fileds = '', $condition = '', $rows = 0) {
- $table = '`'.strtr($table, array(' ' => '', '.' => '`.`', ',' => '`, `')).'`';
- if (empty($fileds)) {
- $fileds = '*';
- }
- if ($rows > 0) {
- $condition .= " LIMIT 0,{$rows}";
- }
- $sql = "SELECT {$fileds} FROM {$table} {$condition}";
- $res = $this->query($sql);
- $this->numRows();
- return $res;
- }
- // 只查询一条记录,返回关联数组
- public function getRows($table, $fileds = '*', $condition = '') {
- $table = '`'.strtr($table, array(' ' => '', '.' => '`.`', ',' => '`, `')).'`';
- if (empty($fileds)) {
- $fileds = '*';
- }
- $this->query("SELECT {$fileds} FROM {$table} {$condition} LIMIT 0,1");
- return $this->fetch();
- }
- /*------------------------------------------------------------------
- * 函数名: addData($table, $data, $values)
- * 作 用: 添加数据
- * 参 数: $table 表名(必填)
- $data 待添加数据,推荐使用数组类型(必填)
- $values 待添加数据值,如果为字符串时使用(选填)
- * 返回值: 布尔
- * 实 例: $DB->addData('mydb',array('user'=>'admin','password'=>'123456','age'=>'18') 数组类型(提示: 这里可以直接用$_POST或者$_GET)
- $DB->addData('mydb','`user`,`password`,`age`',"'admin','123456','18'" 字符串类型
- -------------------------------------------------------------------*/
- public function addData($table = '', $data = array(), $values = '') {
- if (empty($table)) {
- $this->showError('待添加数据表名为空');
- }
- $table = '`'.strtr($table, array(' ' => '', '.' => '`.`', ',' => '`, `')).'`';
- if (empty($data)) {
- $this->showError('待添加数据为空');
- }
- if (is_array($data)) {
- $values = '';
- foreach ($data as $key => $val) {
- $key = trim($key);
- if (!empty($key)) {
- $fileds .= "`{$key}`,";
- $values .= $val ? "'".mysql_escape_string($val)."'," : '"",';
- }
- }
- $fileds = rtrim($fileds, ',');
- $values = rtrim($values, ',');
- } else {
- $fileds = $data;
- }
- return $this->query("INSERT INTO {$table} ({$fileds}) VALUES ({$values})");
- }
- /*------------------------------------------------------------------
- * 函数名: setData($table, $data, $condition, $unQuot)
- * 作 用: 更改数据
- * 参 数: $table 表名(必填)
- $data 待更改数据,可以为数组(必填)
- $condition 更改条件(选填)
- $unQuot 不需要加引号的字段,用于字段的加减运算等情况,多个字段用,分隔或者写入一个数组(选填)
- * 返回值: 布尔
- * 实 例: $DB->setData('mydb',array('user'=>'admin','password'=>'123456','WHERE id=1') 数组类型
- $DB->setData('mydb',"user='admin',password='123456'",'WHERE id=1') 字符串类型
- -------------------------------------------------------------------*/
- public function setData($table = '', $data = '', $condition = '', $unQuot = array()) {
- if (empty($table)) {
- $this->showError('待修改数据表名为空');
- }
- $table = '`'.strtr($table, array(' ' => '', '.' => '`.`', ',' => '`, `')).'`';
- if (empty($data)) {
- $this->showError('待修改数据为空');
- }
- if (is_array($data)) {
- $unQuot = is_array($unQuot) ? $unQuot : explode(',', $unQuot);
- foreach ($data as $key => $val) {
- $val = mysql_escape_string($val);
- $values .= '`'.$key.'`='.(in_array($key, $unQuot) ? "{$val}," : "'{$val}',");
- }
- $values = rtrim($values, ',');
- } else {
- $values = $data;
- }
- return $this->query("UPDATE {$table} SET {$values} {$condition}");
- }
- /*------------------------------------------------------------------
- * 函数名: delData($table, $condition)
- * 作 用: 删除数据
- * 参 数: $table 表名(必填)
- $condition 删除条件(选填)
- * 返回值: 布尔
- * 实 例: $DB->delData('mydb','id=1')
- -------------------------------------------------------------------*/
- public function delData($table, $condition = '') {
- $table = '`'.strtr($table, array(' ' => '', '.' => '`.`', ',' => '`, `')).'`';
- return $this->query("DELETE FROM {$table}".($condition ? " WHERE {$condition}" : ''));
- }
- /*------------------------------------------------------------------
- * 函数名: fetch($result, $type, $arrType)
- * 作 用: 根据从结果集取得的行生成数组
- * 参 数: $result 结果集(选填)
- $type 返回的数组类型,1为mysql_fetch_assoc,2为mysql_fetch_row,3为mysql_fetch_object,默认为mysql_fetch_array(选填)
- $arrType 用于mysql_fetch_array,可以接受以下值: MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH(选填)
- * 返回值: 布尔
- * 实 例: 无
- -------------------------------------------------------------------*/
- public function fetch($result = '', $type = '', $arrType = MYSQL_BOTH) {
- if (empty($result)) {
- $result = $this->result;
- }
- if (!$result) {
- $this->showError('未获取到查询结果', true);
- }
- switch ($type) {
- case 1: // 获取关联数组,使用$row['字段名']
- return mysql_fetch_assoc($result);
- case 2: // 获取数字索引数组,使用$row[0],$row[1],$row[2]
- return mysql_fetch_row($result);
- case 3: // 获取对象数组
- return mysql_fetch_object($result);
- default: // 关联数组,或数字数组,或二者兼有
- return mysql_fetch_array($result, $arrType);
- }
- }
- // 取得结果数据
- public function result($result = '', $rows = 0, $field = '') {
- if (empty($result)) {
- $result = $this->result;
- }
- if (!$result) {
- $this->showError('未获取到查询结果', true);
- }
- return mysql_result($result, $rows, $field);
- }
复制代码
|