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 相关文章推荐
apache+codeigniter 通过.htcaccess做动态二级域名解析
Jul 01 PHP
解析PHP留言本模块主要功能的函数说明(代码可实现)
Jun 25 PHP
PHP登陆后跳转到登陆前页面实现思路及代码
Jan 17 PHP
php递归删除目录下的文件但保留的实例分享
May 10 PHP
php数组中删除元素之重新索引的方法
Sep 16 PHP
PHP将进程作为守护进程的方法
Mar 19 PHP
PHP使用redis实现统计缓存mysql压力的方法
Nov 14 PHP
WordPress主题制作中自定义头部的相关PHP函数解析
Jan 08 PHP
Zend Framework创建自己的动作助手详解
Mar 05 PHP
php格式化json函数示例代码
May 12 PHP
YII2框架中使用yii.js实现的post请求
Apr 09 PHP
基于laravel Request的所有方法详解
Sep 29 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
77A一级收信机修理记
2021/03/02 无线电
漂亮但不安全的CTB
2006/10/09 PHP
解析PHP强制转换类型及远程管理插件的安全隐患
2014/06/30 PHP
ThinkPHP通过AJAX返回JSON的两种实现方法
2014/12/18 PHP
Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
2019/07/24 PHP
javascript 使td内容不换行不撑开
2012/11/29 Javascript
js css 实现遮罩层覆盖其他页面元素附图
2014/09/22 Javascript
JS中Location使用详解
2015/05/12 Javascript
JavaScript中的setUTCDate()方法使用详解
2015/06/11 Javascript
jQuery解决input超多的表单提交
2015/08/10 Javascript
基于BootstrapValidator的Form表单验证(24)
2016/12/12 Javascript
js实现添加删除表格(两种方法)
2017/04/27 Javascript
关于在mongoose中填充外键的方法详解
2017/08/14 Javascript
vue组件Prop传递数据的实现示例
2017/08/17 Javascript
jQuery中ajax获取数据赋值给页面的实例
2017/12/31 jQuery
浅谈vuejs实现数据驱动视图原理
2018/02/23 Javascript
Node.js的进程管理的深入理解
2019/01/09 Javascript
Vue 无限滚动加载指令实现方法
2019/05/28 Javascript
原生js实现抽奖小游戏
2019/06/27 Javascript
在微信小程序中使用mqtt服务的方法
2019/12/13 Javascript
vue-cli3项目配置eslint代码规范的完整步骤
2020/09/10 Javascript
跟老齐学Python之玩转字符串(2)更新篇
2014/09/28 Python
Python随机函数random()使用方法小结
2018/04/29 Python
Python 实现异步调用函数的示例讲解
2018/10/14 Python
Python解决两个整数相除只得到整数部分的实例
2018/11/10 Python
浅析python中while循环和for循环
2019/11/19 Python
css3实现背景颜色渐变让图片不再是唯一的实现方式
2012/12/18 HTML / CSS
美国农场鲜花速递:The Bouqs
2018/07/13 全球购物
Farfetch阿联酋:奢侈品牌时尚购物平台
2019/07/26 全球购物
进程的查看和调度分别使用什么命令
2015/03/25 面试题
js实现弹框效果
2021/03/24 Javascript
融资租赁计划书
2014/04/29 职场文书
爱我中华演讲稿
2014/05/20 职场文书
2014年教师节座谈会发言稿
2014/09/10 职场文书
2014教师“四风问题”对照检查材料思想汇报
2014/09/16 职场文书
2014年后勤管理工作总结
2014/12/01 职场文书