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 相关文章推荐
文件上传类
Oct 09 PHP
MYSQL环境变量设置方法
Jan 15 PHP
PHP计划任务之关闭浏览器后仍然继续执行的函数
Jul 22 PHP
用PHP+MySQL搭建聊天室功能实例代码
Aug 20 PHP
JoshChen_web格式编码UTF8-无BOM的小细节分析
Aug 16 PHP
php创建sprite
Feb 11 PHP
linux中cd命令使用详解
Jan 08 PHP
php制作动态随机验证码
Feb 12 PHP
PHP实现简单搜歌的方法
Jul 28 PHP
PHP实现Google plus的好友拖拽分组效果
Oct 21 PHP
PHP编程快速实现数组去重的方法详解
Jul 22 PHP
php使用curl伪造来源ip和refer的方法示例
May 08 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
打造计数器DIY三步曲(上)
2006/10/09 PHP
php防攻击代码升级版
2010/12/29 PHP
php+mysqli预处理技术实现添加、修改及删除多条数据的方法
2015/01/30 PHP
PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 如何解决
2016/04/17 PHP
PHP输出多个元素的排列或组合的方法
2017/03/14 PHP
PHPExcel实现的读取多工作表操作示例
2020/04/14 PHP
js trim函数 去空格函数与正则集锦
2009/11/20 Javascript
ext jquery 简单比较
2010/04/07 Javascript
js中页面的重新加载(当前页面/上级页面)及frame或iframe元素引用介绍
2013/01/24 Javascript
可简单避免的三个JS发布错误的详细介绍
2013/08/02 Javascript
原生js实现移动开发轮播图、相册滑动特效
2015/04/17 Javascript
js实现简洁的TAB滑动门效果代码
2015/09/06 Javascript
jQuery实现图片走马灯效果的原理分析
2016/01/16 Javascript
jQuery siblings()用法实例详解
2016/04/26 Javascript
vuex 使用文档小结篇
2018/01/11 Javascript
vue-cli3跨域配置的简单方法
2019/09/06 Javascript
[34:44]Liquid vs TNC Supermajor 胜者组 BO3 第二场 6.4
2018/06/05 DOTA
python使用正则表达式检测密码强度源码分享
2014/06/11 Python
python将图片文件转换成base64编码的方法
2015/03/14 Python
Python 基于Twisted框架的文件夹网络传输源码
2016/08/28 Python
python range()函数取反序遍历sequence的方法
2018/06/25 Python
python读取文本中的坐标方法
2018/10/14 Python
Python中is和==的区别详解
2018/11/15 Python
Python中字符串与编码示例代码
2019/05/20 Python
Python OpenCV实现鼠标画框效果
2020/08/19 Python
如何解决安装python3.6.1失败
2020/07/01 Python
深入解析HTML5中的Blob对象的使用
2015/09/08 HTML / CSS
巴西购物网站:Submarino
2020/01/19 全球购物
不同浏览器创建XMLHttpRequest方法有什么不同
2014/11/17 面试题
卫校中专生个人自我评价
2013/09/19 职场文书
金融事务专业毕业生求职信
2014/02/23 职场文书
治庸问责心得体会
2014/09/12 职场文书
领导走群众路线整改措施思想汇报
2014/10/12 职场文书
教师反邪教心得体会
2016/01/15 职场文书
GO语言字符串处理函数之处理Strings包
2022/04/14 Golang
CSS子盒子水平和垂直居中的五种方法
2022/07/23 HTML / CSS