首页 > php教程 > PHP源码 > 正文

emlog数据库操作类

PHP中文网
发布: 2016-05-25 17:09:14
原创
1377人浏览过

<?php
/**
 * 数据库操作类
 *
 * @copyright (c) Emlog All Rights Reserved
 */
 
/**
 * MYSQL数据操方法封装类
 */
 
class MySql {
 
    /**
     * 查询次数
     * @var int
     */
    private $queryCount = 0;
 
    /**
     * 内部数据连接对象
     * @var resourse
     */
    private $conn;
 
    /**
     * 内部数据结果
     * @var resourse
     */
    private $result;
 
    /**
     * 内部实例对象
     * @var object MySql
     */
    private static $instance = null;
 
    /**
     * 构造函数
     */
    private function __construct() {
        if (!function_exists('mysql_connect')) {
            emMsg('服务器PHP不支持MySql数据库');
        }
        if (!$this->conn = @mysql_connect(DB_HOST, DB_USER, DB_PASSWD)) {
            emMsg("连接数据库失败,可能是数据库用户名或密码错误");
        }
        if ($this->getMysqlVersion() > '4.1') {
            mysql_query("SET NAMES 'utf8'");
        }
        @mysql_select_db(DB_NAME, $this->conn) OR emMsg("未找到指定数据库");
    }
 
    /**
     * 静态方法,返回数据库连接实例
     */
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new MySql();
        }
        return self::$instance;
    }
 
    /**
     * 关闭数据库连接
     */
    function close() {
        return mysql_close($this->conn);
    }
 
    /**
     * 发送查询语句
     *
     */
    function query($sql) {
        $this->result = @mysql_query($sql, $this->conn);
        $this->queryCount++;
        if (!$this->result) {
            emMsg("SQL语句执行错误:$sql <br />" . $this->geterror());
        }else {
            return $this->result;
        }
    }
 
    /**
     * 从结果集中取得一行作为关联数组/数字索引数组
     *
     */
    function fetch_array($query , $type = MYSQL_ASSOC) {
        return mysql_fetch_array($query, $type);
    }
 
    function once_fetch_array($sql) {
        $this->result = $this->query($sql);
        return $this->fetch_array($this->result);
    }
 
    /**
     * 从结果集中取得一行作为数字索引数组
     *
     */
    function fetch_row($query) {
        return mysql_fetch_row($query);
    }
 
    /**
     * 取得行的数目
     *
     */
    function num_rows($query) {
        return mysql_num_rows($query);
    }
 
    /**
     * 取得结果集中字段的数目
     */
    function num_fields($query) {
        return mysql_num_fields($query);
    }
    /**
     * 取得上一步 INSERT 操作产生的 ID
     */
    function insert_id() {
        return mysql_insert_id($this->conn);
    }
 
    /**
     * 获取mysql错误
     */
    function geterror() {
        return mysql_error();
    }
 
    /**
     * Get number of affected rows in previous MySQL operation
     */
    function affected_rows() {
        return mysql_affected_rows();
    }
 
    /**
     * 取得数据库版本信息
     */
    function getMysqlVersion() {
        return mysql_get_server_info();
    }
 
    /**
     * 取得数据库查询次数
     */
    function getQueryCount() {
        return $this->queryCount;
    }
}
登录后复制

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号