php mysql数据库操作类


Posted in PHP onJune 04, 2008
<?php 
/* 
 *    mysql数据库 DB类 
 *    @package    db 
 *    @author        yytcpt(无影) 
 *    @version    2008-03-27 
 *    @copyrigth    http://www.d5s.cn/  
 */ 
class db { 
    var $connection_id = ""; 
    var $pconnect = 0; 
    var $shutdown_queries = array(); 
    var $queries = array(); 
    var $query_id = ""; 
    var $query_count = 0; 
    var $record_row = array(); 
    var $failed = 0; 
    var $halt = ""; 
    var $query_log = array(); 
    function connect($db_config){ 
        if ($this->pconnect){ 
            $this->connection_id = mysql_pconnect($db_config["hostname"], $db_config["username"], $db_config["password"]); 
        }else{ 
            $this->connection_id = mysql_connect($db_config["hostname"], $db_config["username"], $db_config["password"]); 
        } 
        if ( ! $this->connection_id ){ 
            $this->halt("Can not connect MySQL Server"); 
        } 
        if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){ 
            $this->halt("Can not connect MySQL Database"); 
        } 
        if ($db_config["charset"]) { 
            @mysql_unbuffered_query("SET NAMES '".$db_config["charset"]."'"); 
        } 
        return true; 
    } 
    //发送SQL 查询,并返回结果集 
    function query($query_id, $query_type='mysql_query'){ 
        $this->query_id = $query_type($query_id, $this->connection_id); 
        $this->queries[] = $query_id; 
        if (! $this->query_id ) { 
            $this->halt("查询失败:\n$query_id"); 
        } 
        $this->query_count++; 
        $this->query_log[] = $str; 
        return $this->query_id; 
    } 
    //发送SQL 查询,并不获取和缓存结果的行 
    function query_unbuffered($sql=""){ 
        return $this->query($sql, 'mysql_unbuffered_query'); 
    } 
    //从结果集中取得一行作为关联数组 
    function fetch_array($sql = ""){ 
        if ($sql == "") $sql = $this->query_id; 
        $this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC); 
        return $this->record_row; 
    } 
    function shutdown_query($query_id = ""){ 
        $this->shutdown_queries[] = $query_id; 
    } 
    //取得结果集中行的数目,仅对 INSERT,UPDATE 或者 DELETE 
    function affected_rows() { 
        return @mysql_affected_rows($this->connection_id); 
    } 
    //取得结果集中行的数目,仅对 SELECT 语句有效 
    function num_rows($query_id="") { 
        if ($query_id == "") $query_id = $this->query_id; 
        return @mysql_num_rows($query_id); 
    } 
    //返回上一个 MySQL 操作中的错误信息的数字编码 
    function get_errno(){ 
        $this->errno = @mysql_errno($this->connection_id); 
        return $this->errno; 
    } 
    //取得上一步 INSERT 操作产生的 ID 
    function insert_id(){ 
        return @mysql_insert_id($this->connection_id); 
    } 
    //得到查询次数 
    function query_count() { 
        return $this->query_count; 
    } 
    //释放结果内存 
    function free_result($query_id=""){ 
           if ($query_id == "") $query_id = $this->query_id; 
        @mysql_free_result($query_id); 
    } 
    //关闭 MySQL 连接 
    function close_db(){ 
        if ( $this->connection_id ) return @mysql_close( $this->connection_id ); 
    } 
    //列出 MySQL 数据库中的表 
    function get_table_names(){ 
        global $db_config; 
        $result = mysql_list_tables($db_config["database"]); 
        $num_tables = @mysql_numrows($result); 
        for ($i = 0; $i < $num_tables; $i++) { 
            $tables[] = mysql_tablename($result, $i); 
        } 
        mysql_free_result($result); 
        return $tables; 
       } 
    //从结果集中取得列信息并作为对象返回,取得所有字段 
    function get_result_fields($query_id=""){ 
           if ($query_id == "") $query_id = $this->query_id; 
        while ($field = mysql_fetch_field($query_id)) { 
            $fields[] = $field; 
        } 
        return $fields; 
       } 
    //错误提示 
    function halt($the_error=""){ 
        $message = $the_error."<br/>\r\n"; 
        $message.= $this->get_errno() . "<br/>\r\n"; 
        $sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")"; 
        @mysql_unbuffered_query($sql); 
        if (DEBUG==true){ 
            echo "<html><head><title>MySQL 数据库错误</title>"; 
            echo "<style type=\"text/css\"><!--.error { font: 11px tahoma, verdana, arial, sans-serif, simsun; }--></style></head>\r\n"; 
            echo "<body>\r\n"; 
            echo "<blockquote>\r\n"; 
            echo "<textarea class=\"error\" rows=\"15\" cols=\"100\" wrap=\"on\" >" . htmlspecialchars($message) . "</textarea>\r\n"; 
            echo "</blockquote>\r\n</body></html>"; 
            exit; 
        } 
    } 
    function __destruct(){ 
        $this->shutdown_queries = array(); 
        $this->close_db(); 
    } 
    function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){ 
        $sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:""); 
        return $sql; 
    } 
    function sql_insert($tbname, $row){ 
        foreach ($row as $key=>$value) { 
            $sqlfield .= $key.","; 
            $sqlvalue .= "'".$value."',"; 
        } 
        return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")"; 
    } 
    function sql_update($tbname, $row, $where){ 
        foreach ($row as $key=>$value) { 
            $sqlud .= $key."= '".$value."',"; 
        } 
        return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where; 
    } 
    function sql_delete($tbname, $where){ 
        return "DELETE FROM `".$tbname."` WHERE ".$where; 
    } 
    //新增加一条记录 
    function row_insert($tbname, $row){ 
        $sql = $this->sql_insert($tbname, $row); 
        return $this->query_unbuffered($sql); 
    } 
    //更新指定记录 
    function row_update($tbname, $row, $where){ 
        $sql = $this->sql_update($tbname, $row, $where); 
        return $this->query_unbuffered($sql); 
    } 
    //删除满足条件的记录 
    function row_delete($tbname, $where){ 
        $sql = $this->sql_delete($tbname, $where); 
        return $this->query_unbuffered($sql); 
    } 
    /*    根据条件查询,返回所有记录 
     *    $tbname 表名, $where 查询条件, $limit 返回记录, $fields 返回字段 
     */ 
    function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){ 
        $sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort); 
        return $this->row_query($sql); 
    } 
    //详细显示一条记录 
    function row_select_one($tbname, $where, $fields="*", $orderby="id"){ 
        $sql = $this->sql_select($tbname, $where, 1, $fields, $orderby); 
        return $this->row_query_one($sql); 
    } 
    function row_query($sql){ 
        $rs     = $this->query($sql); 
        $rs_num = $this->num_rows($rs); 
        $rows = array(); 
        for($i=0; $i<$rs_num; $i++){ 
            $rows[] = $this->fetch_array($rs); 
        } 
        $this->free_result($rs); 
        return $rows; 
    } 
    function row_query_one($sql){ 
        $rs     = $this->query($sql); 
        $row = $this->fetch_array($rs); 
        $this->free_result($rs); 
        return $row; 
    } 
    //计数统计 
    function row_count($tbname, $where=""){ 
        $sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:""); 
        $row = $this->row_query_one($sql); 
        return $row["row_sum"]; 
    } 
} 
?>

很久没有发帖了,把我常用的一些php类文件分享出来。
如果您加了新功能,或者是有改进,请与大家一起分享。
<?php 
    $db_config["hostname"]    = "127.0.0.1";    //服务器地址 
    $db_config["username"]    = "root";        //数据库用户名 
    $db_config["password"]    = "root";        //数据库密码 
    $db_config["database"]    = "wap_blueidea_com";        //数据库名称 
    $db_config["charset"]        = "utf8"; 
    include('db.php'); 
    $db    = new db(); 
    $db->connect($db_config); 
    //例:查询表 table_name 中 cid=1的所有记录。 
    $row = $db->row_select('table_name', 'cid=1'); 
?>

更详细的使用方法,请参考 db类文件中的注释。 
PHP 相关文章推荐
我的论坛源代码(二)
Oct 09 PHP
cmd下运行php脚本
Nov 25 PHP
zf框架的session会话周期及次数限制使用示例
Mar 13 PHP
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
Apr 01 PHP
CI框架中libraries,helpers,hooks文件夹详细说明
Jun 10 PHP
php中限制ip段访问、禁止ip提交表单的代码分享
Aug 22 PHP
PHP多进程编程实例
Oct 15 PHP
Yii扩展组件编写方法实例分析
Jun 29 PHP
php上传大文件设置方法
Apr 14 PHP
微信公众平台开发教程⑤ 微信扫码支付模式介绍
Apr 10 PHP
php 多继承的几种常见实现方法示例
Nov 18 PHP
基于PHP实现邮箱验证激活过程详解
Oct 28 PHP
CodeIgniter php mvc框架 中国网站
May 26 #PHP
五个PHP程序员工具
May 26 #PHP
需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
May 26 #PHP
JS实现php的伪分页
May 25 #PHP
php中iconv函数使用方法
May 24 #PHP
在PHP中使用模板的方法
May 24 #PHP
php读取数据库信息的几种方法
May 24 #PHP
You might like
PHP的Yii框架使用中的一些错误解决方法与建议
2015/08/21 PHP
php类自动装载、链式操作、魔术方法实现代码
2017/07/23 PHP
php通过各种函数判断0和空
2020/07/04 PHP
Thinkphp5.0 框架Model模型简单用法分析
2019/10/11 PHP
TP5框架实现签到功能的方法分析
2020/04/05 PHP
Extjs TriggerField在弹出窗口显示不出问题的解决方法
2010/01/08 Javascript
JQuery伸缩导航练习示例
2013/11/13 Javascript
JQuery解析HTML、JSON和XML实例详解
2014/03/29 Javascript
jquery修改网页背景颜色通过css方法实现
2014/06/06 Javascript
让人蛋疼的JavaScript语法特性
2014/09/30 Javascript
jquery自适应布局的简单实例
2016/05/28 Javascript
基于JavaScript实现全选、不选和反选效果
2017/02/15 Javascript
javascript使用btoa和atob来进行Base64转码和解码
2017/03/20 Javascript
基于Vue的ajax公共方法(详解)
2018/01/20 Javascript
利用JS动态生成隔行换色HTML表格的两种方法
2018/10/09 Javascript
微信小程序页面调用自定义组件内的事件详解
2019/09/12 Javascript
Centos7 安装Node.js10以上版本的方法步骤
2019/10/15 Javascript
解决 window.onload 被覆盖的问题方法
2020/01/14 Javascript
jQuery实现html可联动的百分比进度条
2020/03/26 jQuery
vue从后台渲染文章列表以及根据id跳转文章详情详解
2020/12/14 Vue.js
Python中实现远程调用(RPC、RMI)简单例子
2014/04/28 Python
python实现bucket排序算法实例分析
2015/05/04 Python
Ubuntu安装Jupyter Notebook教程
2017/10/18 Python
Python列表对象实现原理详解
2019/07/01 Python
python调用matplotlib模块绘制柱状图
2019/10/18 Python
python实现批量转换图片为黑白
2020/06/16 Python
详解WebSocket跨域问题解决
2018/08/06 HTML / CSS
英国自行车商店:AW Cycles
2021/02/24 全球购物
往来会计岗位职责
2013/12/19 职场文书
八一建军节感言
2014/02/28 职场文书
计算机实训报告总结
2014/11/05 职场文书
2014年人大工作总结
2014/12/10 职场文书
个人年终总结范文
2015/03/09 职场文书
英文自荐信范文
2015/03/25 职场文书
初二物理教学反思
2016/02/19 职场文书
2016年暑期社会实践活动总结报告
2016/04/06 职场文书