Mysql数据库操作类( 1127版,提供源码下载 )


Posted in PHP onDecember 02, 2010

Mysql.class.php 下载

<?php 
class Mysql { 
private $db_host; //主机地址 
private $db_user; //用户名 
private $db_pass; //连接密码 
private $db_name; //名称 
private $db_charset; //编码 
private $conn; 
public $debug=false;//调试开关,默认关闭 
private $query_id; //用于判断sql语句是否执行成功 
private $result; //结果集 
private $num_rows; //结果集中行的数目,仅对select有效 
private $insert_id; //上一步 INSERT 操作产生的 ID 
// 构造/析构函数 
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) { 
$this->db_host = $db_host ; 
$this->db_user = $db_user ; 
$this->db_pass = $db_pass ; 
$this->db_name = $db_name ; 
$this->db_charset = $db_charset ; 
$this->conn = $conn ; 
$this->connect(); 
} 
function __destruct () { 
@mysql_close($this->conn); 
} 
// 连接/选择数据库 
public function connect () { 
if ($this->conn == 'pconn') { 
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass); 
} else { 
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
} 
if (!$this->conn) { 
$this->show_error('数据库-连接失败:用户名或密码错误!'); 
} 
if (!@mysql_select_db($this->db_name,$this->conn)) { 
$this->show_error("数据库-选择失败:数据库 $this->db_name 不可用"); 
} 
mysql_query("SET NAMES $this->db_charset"); 
return $this->conn; 
} 
// query方法 
public function query ($sql) { 
if ($this->query_id) $this->free_result(); 
$this->query_id = @mysql_query($sql,$this->conn); 
if (!$this->query_id) $this->show_error("SQL语句 <b>\"$sql\"</b> 执行时遇到错误"); 
return $this->query_id; 
} 
// 显示详细错误信息 
public function show_error ($msg) { 
if($this->debug){ 
$errinfo = mysql_error(); 
echo "错误:$msg <br/> 返回:$errinfo<p>"; 
}else{ 
echo '<p>出现错误!<p>'; 
} 
} 
// 获得query执行成功与否的信息 
public function get_query_info($info){ 
if ($this->query_id) { 
echo $info; 
} 
} 
// 查询所有 
public function findall ($table_name) { 
$this->query("select * from $table_name"); 
} 
// mysql_fetch_array 
public function fetch_array () { 
if ($this->query_id) { 
$this->result = mysql_fetch_array($this->query_id); 
return $this->result; 
} 
} 
// ...... 
public function fetch_assoc () { 
if ($this->query_id) { 
$this->result = mysql_fetch_assoc($this->query_id); 
return $this->result; 
} 
} 
public function fetch_row () { 
if ($this->query_id) { 
$this->result = mysql_fetch_row($this->query_id); 
return $this->result; 
} 
} 
public function fetch_object () { 
if ($this->query_id) { 
$this->result = mysql_fetch_object($this->query_id); 
return $this->result; 
} 
} 
// 获取 num_rows 
public function num_rows () { 
if ($this->query_id) { 
$this->num_rows = mysql_num_rows($this->query_id); 
return $this->num_rows; 
} 
} 
// 获取 insert_id 
public function insert_id () { 
return $this->insert_id = mysql_insert_id(); 
} 
// 显示共有多少张表 
public function show_tables () { 
$this->query("show tables"); 
if ($this->query_id) { 
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>"; 
$i = 1; 
while ($row = $this->fetch_array($this->query_id)){ 
echo "$i -- $row[0]<br/>"; 
$i ++; 
} 
} 
} 
// 显示共有多少个数据库 
public function show_dbs(){ 
$this->query("show databases"); 
if ($this->query_id) { 
echo "共有数据库 ".$this->num_rows($this->query_id)." 个<br/>"; 
$i = 1; 
while ($this->row = $this->fetch_array($this->query_id)){ 
echo "$i -- ".$this->row[Database]."<br />"; 
$i ++; 
} 
} 
} 
// 删除数据库:返回删除结果 
public function drop_db ($db_name='') { 
if ($db_name == '') { 
$db_name = $this->db_name;//默认删除当前数据库 
$this->query("DROP DATABASE $db_name"); 
}else { 
$this->query("DROP DATABASE $db_name"); 
} 
if ($this->query_id) { 
return "数据库 $db_name 删除成功"; 
}else { 
$this->show_error("数据库 $db_name 删除失败"); 
} 
} 
// 删除数据表:返回删除结果 
public function drop_table ($table_name) { 
$this->query("DROP TABLE $table_name"); 
if ($this->query_id) { 
return "数据表 $table_name 删除成功"; 
}else { 
$this->show_error("数据表 $table_name 删除失败"); 
} 
} 
// 创建数据库 
public function create_db ($db_name) { 
$this->query("CREATE DATABASE $db_name"); 
if($this->query_id){ 
return "数据库 $db_name 创建成功"; 
}else { 
$this->show_error("数据库 $db_name 创建失败"); 
} 
} 
// 获取数据库版本 
public function get_info(){ 
echo mysql_get_server_info(); 
} 
// 释放内存 
public function free_result () { 
if ( @mysql_free_result($this->query_id) ) 
unset ($this->result); 
$this->query_id = 0; 
} 
} // End class 
?>

PHP 相关文章推荐
基于mysql的论坛(3)
Oct 09 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十三)
Jun 26 PHP
php实现Linux服务器木马排查及加固功能
Dec 29 PHP
PHP+MySQL修改记录的方法
Jan 21 PHP
php用户注册信息验证正则表达式
Nov 12 PHP
php+ajax无刷新分页实例详解
Dec 07 PHP
php实现在线通讯录功能(附源码)
May 13 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
详解PHP swoole process的使用方法
Aug 26 PHP
PHP命名空间定义与用法实例分析
Aug 14 PHP
php设计模式之代理模式分析【星际争霸游戏案例】
Mar 23 PHP
基于PHP实现用户登录注册功能的详细教程
Aug 04 PHP
PHP分页函数代码(简单实用型)
Dec 02 #PHP
php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
Dec 02 #PHP
php小偷相关截取函数备忘
Nov 28 #PHP
php与paypal整合方法
Nov 28 #PHP
网站用php实现paypal整合方法
Nov 28 #PHP
paypal即时到账php实现代码
Nov 28 #PHP
解析PayPal支付接口的PHP开发方式
Nov 28 #PHP
You might like
SONY SRF-40W电路分析
2021/03/02 无线电
php通过strpos查找字符串出现位置的方法
2015/03/17 PHP
PHP 年月日的三级联动实例代码
2017/05/24 PHP
PHP实现读取文件夹及批量重命名文件操作示例
2019/04/15 PHP
yii框架结合charjs实现统计30天数据的方法
2020/04/04 PHP
Firebug入门指南(Firefox浏览器)
2010/08/21 Javascript
javascript实现字符串反转的方法
2015/02/05 Javascript
简介JavaScript中的setTime()方法的使用
2015/06/11 Javascript
JavaScript 七大技巧(二)
2015/12/13 Javascript
BootStrap 智能表单实战系列(五) 表单依赖插件处理
2016/06/13 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
自己封装的一个原生JS拖动方法(推荐)
2016/11/22 Javascript
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
详解angular脏检查原理及伪代码实现
2018/06/08 Javascript
基于Koa2写个脚手架模拟接口服务的方法
2018/11/27 Javascript
vue element-ui之怎么封装一个自己的组件的详解
2019/05/20 Javascript
iview的table组件自带的过滤器实现
2019/07/12 Javascript
vue实现购物车结算功能
2020/06/18 Javascript
理解JavaScript中的对象
2020/08/25 Javascript
JS实现拖拽元素时与另一元素碰撞检测
2020/08/27 Javascript
vue基于Echarts的拖拽数据可视化功能实现
2020/12/04 Vue.js
Javascript生成器(Generator)的介绍与使用
2021/01/31 Javascript
[01:51]DAC趣味视频-如何成为职业选手.mp4
2017/04/02 DOTA
python缩进区别分析
2014/02/15 Python
浅析Python的Django框架中的Memcached
2015/07/23 Python
举例讲解Python设计模式编程中的访问者与观察者模式
2016/01/26 Python
Java及python正则表达式详解
2017/12/27 Python
Python高级特性与几种函数的讲解
2019/03/08 Python
学习和使用python的13个理由
2019/07/30 Python
Python facenet进行人脸识别测试过程解析
2019/08/16 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
HEMA英国:荷兰原创设计
2018/08/28 全球购物
《观舞记》教学反思
2014/04/16 职场文书
完美的中文自荐信
2014/05/24 职场文书
三问三解心得体会
2014/09/05 职场文书
繁星春水读书笔记
2015/06/30 职场文书