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 相关文章推荐
整理的一些实用WordPress后台MySQL操作命令
Jan 07 PHP
CMS中PHP判断系统是否已经安装的方法示例
Jul 26 PHP
php冒泡排序与快速排序实例详解
Dec 07 PHP
PHP使用内置函数生成图片的方法详解
May 09 PHP
WordPress过滤垃圾评论的几种主要方法小结
Jul 11 PHP
Yii框架批量插入数据扩展类的简单实现方法
May 23 PHP
利用PHPStorm如何开发Laravel应用详解
Aug 30 PHP
PHP实现微信提现功能
Sep 30 PHP
PHP封装的page分页类定义与用法完整示例
Dec 24 PHP
详解PHP队列的实现
Mar 14 PHP
Laravel 验证码认证学习记录小结
Dec 20 PHP
PHP代码覆盖率统计详解
Jul 22 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图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
2010/12/02 PHP
ThinkPHP自定义Redis处理SESSION的实现方法
2016/05/16 PHP
PHP基于cookie实现统计在线人数功能示例
2019/01/16 PHP
Windows服务器中PHP如何安装redis扩展
2019/09/27 PHP
ThinkPHP 框架实现的读取excel导入数据库操作示例
2020/04/14 PHP
Javascript load Page,load css,load js实现代码
2010/03/31 Javascript
JavaScript中setUTCMilliseconds()方法的使用详解
2015/06/12 Javascript
jquery中toggle函数交替使用问题
2015/06/22 Javascript
延时加载JavaScript代码提高速度
2015/12/27 Javascript
Js与Jq获取浏览器和对象值的方法
2016/03/18 Javascript
移动端利用H5实现压缩图片上传功能
2017/03/29 Javascript
微信小程序 navbar实例详解
2017/05/11 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
2018/08/25 Javascript
微信小程序实现3D轮播图效果(非swiper组件)
2019/09/21 Javascript
关于ligerui子页面关闭后,父页面刷新,重新加载的方法
2019/09/27 Javascript
解决vue 表格table列求和的问题
2019/11/06 Javascript
vscode 使用Prettier插件格式化配置使用代码详解
2020/08/10 Javascript
js仿淘宝放大镜效果
2020/12/28 Javascript
python BeautifulSoup设置页面编码的方法
2015/04/03 Python
安装Python的web.py框架并从hello world开始编程
2015/04/25 Python
Python探索之静态方法和类方法的区别详解
2017/10/27 Python
Python Pandas批量读取csv文件到dataframe的方法
2018/10/08 Python
python打开文件的方式有哪些
2020/06/29 Python
丽笙酒店官方网站:Radisson Hotels
2019/05/07 全球购物
应届生法律顾问求职信
2013/11/19 职场文书
父亲追悼会答谢词
2014/01/17 职场文书
农民工工资支付承诺函
2014/03/31 职场文书
村长反四风问题个人对照检查材料
2014/09/21 职场文书
重阳节演讲稿:尊敬帮助老人 弘扬传统美德
2014/09/25 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
护士个人总结范文
2015/02/13 职场文书
导游词之太行山青龙峡
2020/01/14 职场文书
python tkinter模块的简单使用
2021/04/07 Python
浅谈Python中的函数(def)及参数传递操作
2021/05/25 Python
Python集合的基础操作
2021/11/01 Python
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL