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
如何在PHP中使用Oracle数据库(1)
Oct 09 PHP
Ha0k 0.3 PHP 网页木马修改版
Oct 11 PHP
PHP imagecreatefrombmp 从BMP文件或URL新建一图像
Jul 16 PHP
PHP命名空间(namespace)的动态访问及使用技巧
Aug 18 PHP
Yii不依赖Model的表单生成器用法实例
Dec 04 PHP
PHP远程调试之XDEBUG
Dec 29 PHP
php生成mysql的数据字典
Jul 07 PHP
手把手编写PHP框架 深入了解MVC运行流程
Sep 19 PHP
PHP简单数据库操作类实例【支持增删改查及链式操作】
Oct 10 PHP
浅析PHP中的 inet_pton 网络函数
Dec 16 PHP
PHP实现随机发放扑克牌
Apr 21 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_Flame(Version:Progress)的原代码
2006/10/09 PHP
PHP面向对象概念
2011/11/06 PHP
php实现递归抓取网页类实例
2015/04/03 PHP
用js实现的自定义的对话框的实现代码
2010/03/21 Javascript
document.execCommand()的用法小结
2014/01/08 Javascript
js生成的验证码的实现与技术分析
2014/09/17 Javascript
JavaScript导出Excel实例详解
2014/11/25 Javascript
原生js实现移动端瀑布流式代码示例
2015/12/18 Javascript
js实现延迟加载的几种方法
2017/04/24 Javascript
详解如何在nuxt中添加proxyTable代理
2018/08/10 Javascript
node.js基于socket.io快速实现一个实时通讯应用
2019/04/23 Javascript
vue配置nprogress实现页面顶部进度条
2019/09/21 Javascript
Vue ElementUI实现:限制输入框只能输入正整数的问题
2020/07/31 Javascript
SpringBoot+Vue开发之Login校验规则、实现登录和重置事件
2020/10/19 Javascript
Python通过future处理并发问题
2017/10/17 Python
Python使用matplotlib实现绘制自定义图形功能示例
2018/01/18 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2019/01/19 Python
Python使用dict.fromkeys()快速生成一个字典示例
2019/04/24 Python
Python安装Flask环境及简单应用示例
2019/05/03 Python
Python 实现数据结构中的的栈队列
2019/05/16 Python
PyQt5创建一个新窗口的实例
2019/06/20 Python
Flask框架实现的前端RSA加密与后端Python解密功能详解
2019/08/13 Python
PyCharm使用之配置SSH Interpreter的方法步骤
2019/12/26 Python
让IE可以变相支持CSS3选择器
2010/01/21 HTML / CSS
一款纯css3实现的漂亮的404页面的实例教程
2014/11/27 HTML / CSS
美国顶级品牌男士大码服装店:DXL
2017/08/30 全球购物
酒店值班经理的工作职责范本
2014/02/18 职场文书
2014年国庆晚会主持词
2014/09/19 职场文书
党的群众路线教育实践活动总结材料
2014/10/30 职场文书
酒店收银员岗位职责
2015/04/07 职场文书
公司新员工欢迎词
2015/09/30 职场文书
Python通过m3u8文件下载合并ts视频的操作
2021/04/16 Python
Python控制台输出俄罗斯方块的方法实例
2021/04/17 Python
为Java项目添加Redis缓存的方法
2021/05/18 Redis
python之np.argmax()及对axis=0或者1的理解
2021/06/02 Python
缓存替换策略及应用(以Redis、InnoDB为例)
2021/07/25 Redis