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 相关文章推荐
使用NetBeans + Xdebug调试PHP程序的方法
Apr 12 PHP
php中is_null,empty,isset,unset 的区别详细介绍
Apr 28 PHP
php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
Sep 28 PHP
php生成excel列序号代码实例
Dec 24 PHP
php 启动报错如何解决
Jan 17 PHP
PHP缓存集成库phpFastCache用法
Dec 15 PHP
php通过asort()给关联数组按照值排序的方法
Mar 18 PHP
smarty的section嵌套循环用法示例
May 28 PHP
PHP自定义函数实现格式化秒的方法
Sep 14 PHP
PHP使用Redis替代文件存储Session的方法
Feb 15 PHP
php中str_pad()函数用法分析
Mar 28 PHP
Laravel网站打开速度优化的方法汇总
Jul 16 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
PHP HTML代码串 截取实现代码
2009/06/29 PHP
完美解决phpdoc导出文档中@package的warning及Error的错误
2016/05/17 PHP
PHP使Laravel为JSON REST API返回自定义错误的问题
2018/10/16 PHP
我见过最全的个人js加解密功能页面
2007/12/12 Javascript
Web开发者必备的12款超赞jQuery插件
2010/12/03 Javascript
一个级联菜单代码学习及removeClass与addClass的应用
2013/01/24 Javascript
jquery修改属性值实例代码(设置属性值)
2014/01/06 Javascript
IE中getElementsByName()对有些元素无效的解决方案
2014/09/28 Javascript
jquery调取json数据实现省市级联的方法
2015/01/29 Javascript
举例讲解AngularJS中的模块
2015/06/17 Javascript
JS实现队列与堆栈的方法
2016/04/21 Javascript
JavaScript实现的鼠标响应颜色渐变效果完整实例
2017/02/18 Javascript
nodejs创建简易web服务器与文件读写的实例
2017/09/07 NodeJs
使用html+js+css 实现页面轮播图效果(实例讲解)
2017/09/21 Javascript
JS实现DOM节点插入操作之子节点与兄弟节点插入操作示例
2018/07/30 Javascript
生产制造追溯系统之再说条码打印
2019/06/03 Javascript
微信小程序项目总结之记账小程序功能的实现(包括后端)
2019/08/20 Javascript
WEB前端性能优化的7大手段详解
2020/02/04 Javascript
[02:05]2014DOTA2西雅图国际邀请赛 BBC第二天小组赛总结
2014/07/11 DOTA
python字典get()方法用法分析
2015/04/17 Python
Python3导入CSV文件的实例(跟Python2有些许的不同)
2018/06/22 Python
python字符串下标与切片及使用方法
2020/02/13 Python
python里glob模块知识点总结
2021/01/05 Python
柏林通行证:Berlin Pass
2018/04/11 全球购物
英国蛋糕装饰用品一站式商店:Craft Company
2019/03/18 全球购物
英国马莎百货印度官网:Marks & Spencer印度
2020/10/08 全球购物
创业计划书怎样才能打动风投
2014/01/01 职场文书
《孔子游春》教学反思
2014/02/25 职场文书
成本会计实训报告
2014/11/05 职场文书
五年级学生期末评语
2014/12/26 职场文书
家装业务员岗位职责
2015/04/03 职场文书
付款证明格式范文
2015/06/19 职场文书
建筑工程挂靠协议书
2016/03/23 职场文书
经典格言警句:没有热忱,世间便无进步
2019/11/13 职场文书
深入讲解数据库中Decimal类型的使用以及实现方法
2022/02/15 MySQL
使用python绘制横竖条形图
2022/04/21 Python