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

实用PDO MYSQL数库操作类

PHP中文网
发布: 2016-05-25 17:13:50
原创
1340人浏览过

配制 

<?php
/*配制*/
return array (
'DB_Default'=>'Default',//默认连接
/* 主数据 */
'Default' => 
array (
'DB_HOST' => '127.0.0.1', // 服务器地址
'DB_NAME' => '8now_invoicing', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '', // 密码
'DB_PREFIX' => '8now_', // 数据库表前缀
'DB_CHARSET' => 'utf8' ), // 数据库编码默认采用utf8
);
登录后复制
<?php
/**
 * 数据库操作类
 * @author 角度 QQ:1286522207
 *
 */
 
class db {
    public $data_Default;
    private $DB_HOST;
    private $DB_NAME;
    private $DB_USER;
    private $DB_PWD;
    private $DB_PREFIX;
    private $DB_CHARSET;
    public $Limit = " LIMIT 1";
    public $OrderBy;
    public $and;
    public $dbname;
    protected $sign = array ('=', '<>', '>', '<', '>=', '<=' );
    function __construct($db) {
        $dbmysql = include ROOT_CONFIG . 'db.php';
        $dbconfig = $dbmysql [$db];
        $this->DB_HOST = $dbconfig ['DB_HOST'];
        $this->DB_NAME = $dbconfig ['DB_NAME'];
        $this->DB_USER = $dbconfig ['DB_USER'];
        $this->DB_PWD = $dbconfig ['DB_PWD'];
        $this->DB_PREFIX = $dbconfig ['DB_PREFIX'];
        $this->DB_CHARSET = $dbconfig ['DB_CHARSET'];
    }
    private function pdo_db() {
        try {
            return new PDO ( "mysql:host=$this->DB_HOST;dbname=$this->DB_NAME", $this->DB_USER, 
            $this->DB_PWD, array (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '$this->DB_CHARSET'",
             PDO::ATTR_PERSISTENT => true ) );
        } catch ( Exception $e ) {
            echo '数据库连接失败,详情: ' . $e->getMessage () . ' 请在/config/db.php中配制正确的数据库连接信息';
            exit ();
        }
     
    }
    public function M($name) {
        $this->dbname = $this->DB_PREFIX . $name;
    }
    /**
     * 处理条件
     */
    public function where($where) {
        if (! $this->and)
            $this->and = ' and';
        if (is_array ( $where )) {
            foreach ( $where as $key => $row ) {
                $w [] = " `$key`='$row'";
            }
            $sql = " WHERE (" . trim ( implode ( $this->and, $w ) ) . ") ";
        } elseif (! $where) {
            return false;
        } else {
            $sql = " WHERE " . $where;
        }
        return $sql;
    }
    /**
     * 处理数据
     */
    private function date($date) {
        if (is_array ( $date )) {
            foreach ( $date as $key => $row ) {
                $d [] = "`" . $key . "`='" . $row . "'";
            }
            return implode ( ',', $d );
        } else {
            return $date;
        }
    }
    /**
     * 处理插入数据
     */
    public function Upvalues($date) {
        if (is_array ( $date )) {
            foreach ( $date as $key => $row ) {
                $k [] = "`" . $key . "`";
                $r [] = "'" . $row . "'";
            }
            $k = "(" . implode ( ',', $k ) . ")";
            $r = "(" . implode ( ',', $r ) . ")";
            return $k . ' VALUES ' . $r;
        } else {
            return false;
        }
    }
    /**
     * 排序
     * @param 列 $Row
     * @param ASC or DESC$ORDER
     */
    public function OrderBy($Row = '', $ORDER = 'DESC') {
        $this->OrderBy = "ORDER BY `$Row` $ORDER";
    }
    /**
     * 执行查询语句 SELECT
     */
    public function query($sql) {
        return empty ( $sql ) ? exit ( '必须指定SQL语句' ) : $this->pdo_db ()->query ( $sql );
    }
    /**
     * 执行 INSERT、UPDATE、DELETE
     */
    public function exec($sql) {
        return empty ( $sql ) ? exit ( '必须指定SQL语句' ) : $this->pdo_db ()->exec ( $sql );
    }
    /**
     * 查询个数
     */
    public function Limit($a = NULL, $b = NULL) {
        $this->Limit = Filter::IntegerFilter ( $a ) && Filter::IntegerFilter ( $b ) ? " LIMIT $a,$b" : '';
    }
    /**
     * 处理多个查询条件
     * 
     */
    public function LIKEMultipleQueries($LIKE, $W) {
        $LIKE = array_filter ( explode ( ' ', trim ( $LIKE ) ) );
        if (is_array ( $LIKE )) {
            foreach ( $LIKE as $row ) {
                $l [] = "`$W` LIKE  '%" . trim ( $row ) . "%'";
            }
            return '(' . implode ( ' OR ', $l ) . ')';
        }
    }
    /**
     * 点亮关键字
     */
    public $SearchLightKey = array ();
    public function SearchLightKey($array, $q) {
        foreach ( $array as $s ) {
            foreach ( $q as $key => $row ) {
                $row = array_filter ( explode ( ' ', trim ( $row ) ) );
                if ($s [$key]) {
                    foreach ( $row as $r ) {
                        $s [$key] = preg_replace ( "/(" . trim ( $r ) . ")/i", 
                        "<font style='color: #FF9900'>\1</font>", $s [$key] );
                    }
                }
            }
            $this->SearchLightKey [] = $s;
        }
    }
    /**快捷操作开始**/
    /**
     * 查询一条数据
     */
    public function find($where, $Row = '*') {
        $sql = "SELECT $Row FROM `$this->dbname` " . $this->where ( $where ) . $this->OrderBy . $this->Limit;
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 查询多条数据
     */
    public function findAll($where, $Row = '*') {
        $sql = "SELECT $Row FROM `$this->dbname` " . $this->where ( $where ) . $this->OrderBy . $this->Limit;
        return $this->pdo_db ()->query ( $sql )->fetchAll ();
    }
    /**
     * 查询总数
     */
    public function findsum($where, $Row) {
        $sql = "SELECT SUM($Row) as $Row FROM `$this->dbname` " . $this->where ( $where ) . " LIMIT 1";
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 查询个数
     */
    public function findcount($where, $Row) {
        $sql = "SELECT COUNT($Row) as $Row FROM `$this->dbname` " . $this->where ( $where ) . " LIMIT 1 ";
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 更新数据
     */
    public function update($date, $where) {
        $sql = "UPDATE `$this->dbname` SET " . $this->date ( $date ) . $this->where ( $where ) . ";";
        return $this->pdo_db ()->exec ( $sql );
    }
    /**
     * 插入数据
     */
    public function insert($date) {
        $sql = "INSERT INTO `$this->dbname` " . $this->Upvalues ( $date ) . ";";
        $pdo = $this->pdo_db ();
        return $pdo->exec ( $sql ) ? $pdo->lastInsertId () : false;
    }
    /**
     * 删除数据
     */
    public function delete($where) {
        $sql = "DELETE FROM `$this->dbname` " . $this->where ( $where ) . $this->Limit . ";";
        return $this->pdo_db ()->exec ( $sql );
    }
}
登录后复制

 以上就是实用PDO MYSQL数库操作类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

YFCMS企业网站管理系统EXT4.0
YFCMS企业网站管理系统EXT4.0

功能介绍:后台功能介绍1.系统管理:(1)基本信息管理包括网站名称,域名,管理员昵称,联系电话,邮箱和网站关键字等等的设置。(2) 密码修改 系统管理员密码修改(3)后台登陆记录查看2.新闻管理:(1)新闻管理包括新闻的修改删除(2)添加新闻可添加文字新闻和图片新闻,采用EWEB编辑器操作简便(3)新闻分类管理可自由的添加和删除新闻分类3.产品管理:(1)管理产品可对数据库现有进行修改和删除(2)

YFCMS企业网站管理系统EXT4.0 0
查看详情 YFCMS企业网站管理系统EXT4.0

相关标签:
最佳 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号