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 相关文章推荐
用定制的PHP应用程序来获取Web服务器的状态信息
Oct 09 PHP
php查看session内容的函数
Aug 27 PHP
PHP查询MySQL大量数据的时候内存占用分析
Jul 22 PHP
深入php define()函数以及defined()函数的用法详解
Jun 05 PHP
javascript some()函数用法详解
Nov 13 PHP
php计算两个整数的最大公约数常用算法小结
Mar 05 PHP
php文件操作相关类实例
Jun 18 PHP
Symfony2实现在controller中获取url的方法
Mar 18 PHP
Zend Framework入门教程之Zend_Mail用法示例
Dec 08 PHP
PHP实现在对象之外访问其私有属性private及保护属性protected的方法
Nov 20 PHP
laravel 框架配置404等异常页面
Jan 07 PHP
thinkphp 框架数据库切换实现方法分析
May 18 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
用来解析.htgroup文件的PHP类
2012/09/05 PHP
php curl 伪造IP来源的实例代码
2012/11/01 PHP
php中运用http调用的GET和POST方法示例
2014/09/29 PHP
详解PHP中cookie和session的区别及cookie和session用法小结
2016/06/12 PHP
完美解决在ThinkPHP控制器中命名空间的问题
2017/05/05 PHP
Firefox和IE兼容性问题及解决方法总结
2013/10/08 Javascript
多选列表框动态添加,移动,删除,全选等操作的简单实例
2014/01/13 Javascript
浅谈JS日期(Date)处理函数
2014/12/07 Javascript
Backbone.js的一些使用技巧
2015/07/01 Javascript
jQuery实现的placeholder效果完整实例
2016/08/02 Javascript
Javascript 创建类并动态添加属性及方法的简单实现
2016/10/20 Javascript
ReactNative短信验证码倒计时控件的实现代码
2017/07/20 Javascript
js模拟百度模糊搜索的实例
2017/08/04 Javascript
create-react-app安装出错问题解决方法
2018/09/04 Javascript
微信小程序如何通过用户授权获取手机号(getPhoneNumber)
2020/01/21 Javascript
vue 项目软键盘回车触发搜索事件
2020/09/09 Javascript
手动实现vue2.0的双向数据绑定原理详解
2021/02/06 Vue.js
python实现根据窗口标题调用窗口的方法
2015/03/13 Python
用于统计项目中代码总行数的Python脚本分享
2015/04/21 Python
谈谈如何手动释放Python的内存
2016/12/17 Python
python WindowsError的错误代码详解
2017/07/23 Python
解决Ubuntu pip 安装 mysql-python包出错的问题
2018/06/11 Python
python实现自动发送邮件
2018/06/20 Python
python使用__slots__让你的代码更加节省内存
2018/09/05 Python
Python实现数据结构线性链表(单链表)算法示例
2019/05/04 Python
Python使用get_text()方法从大段html中提取文本的实例
2019/08/27 Python
python使用requests库爬取拉勾网招聘信息的实现
2020/11/20 Python
使用postMessage让 iframe自适应高度的方法示例
2019/10/08 HTML / CSS
Perfume’s Club美国官网:西班牙第一家在线美容店
2020/06/10 全球购物
医院总经理职责
2013/12/26 职场文书
租房合同协议书
2014/04/09 职场文书
心理咨询专业自荐信
2014/07/07 职场文书
2014年加油站工作总结
2014/12/04 职场文书
大学生村官工作心得体会
2016/01/23 职场文书
大学社团活动总结怎么写
2019/06/21 职场文书
mysql连接查询中and与where的区别浅析
2021/07/01 MySQL