• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > pdo db 操作类

pdo db 操作类

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了代码片段,代码分享,PHP代码分享,Java代码分享,Ruby代码分享,Python代码分享,HTML代码分享,CSS代等相关知识,希望本文的分享对您有所帮助
getAll("SELECT * FROM live_userinfo_base limit 10");
/**
 * 插入
 */
//$sss = $db->insert('live_user_online', [
//    'userid' => 1111,
//    'name' => 'aaaaaaa',
//    'roomid' => 1006,
//    'role'  => 3,
//    'level' => 3,
//    'ltime' => 21312,
//    'regIp' => 23432432
//]);
/**
 * 更新
 */
//$sss = $db->update('live_user_online', [
//    'name' => 'aaaaaaasdfsdfsdfaaaaaaaaaaa',
//    'roomid' => 1006,
//    'role'  => 3,
//    'level' => 3,
//    'ltime' => 21312,
//    'regIp' => 23432432
//], 'userid = 1111', false);
/**
 * 删除
 */
//$sss = $db->delete('live_user_online', 'userid = 1111', false);
//print_r($sss);
/**
 * Db 简单数据库操作类
 */
/**
 * MyPDO
 * @author Jason.Wei 
 * @license http://www.sunbloger.com/
 * @version 5.0 utf8
 */
class Db
{
    protected static $_instance = null;
    protected $dbName = '';
    protected $dsn;
    protected $dbh;
    /**
     * 构造
     *
     * @return MyPDO
     */
    private function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
    {
        try {
            $this->dsn = 'mysql:host='.$dbHost.';dbname='.$dbName;
            $this->dbh = new PDO($this->dsn, $dbUser, $dbPasswd);
            $this->dbh->exec('SET character_set_connection='.$dbCharset.', character_set_results='.$dbCharset.', character_set_client=binary');
        } catch (PDOException $e) {
            $this->outputError($e->getMessage());
        }
    }
    /**
     * 防止克隆
     *
     */
    private function __clone() {}
    /**
     * Singleton instance
     *
     * @return Object
     */
    public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
    {
        if (self::$_instance === null) {
            self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
        }
        return self::$_instance;
    }
    public function getOne($strSql, $debug = false)
    {
        return $this->query($strSql, $queryMode = 'Row', $debug = false);
    }
    public function getAll($strSql, $debug = false)
    {
        return $this->query($strSql, $queryMode = 'All', $debug = false);
    }
    /**
     * Query 查询
     *
     * @param String $strSql SQL语句
     * @param String $queryMode 查询方式(All or Row)
     * @param Boolean $debug
     * @return Array
     */
    private function query($strSql, $queryMode = 'All', $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $recordset = $this->dbh->query($strSql);
        $this->getPDOError();
        if ($recordset) {
            $recordset->setFetchMode(PDO::FETCH_ASSOC);
            if ($queryMode == 'All') {
                $result = $recordset->fetchAll();
            } elseif ($queryMode == 'Row') {
                $result = $recordset->fetch();
            }
        } else {
            $result = null;
        }
        return $result;
    }
    /**
     * Update 更新
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param String $where 条件
     * @param Boolean $debug
     * @return Int
     */
    public function update($table, $arrayDataValue, $where = '', $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        if ($where) {
            $strSql = '';
            foreach ($arrayDataValue as $key => $value) {
                $strSql .= ", `$key`='$value'";
            }
            $strSql = substr($strSql, 1);
            $strSql = "UPDATE `$table` SET $strSql WHERE $where";
        } else {
            $strSql = "REPLACE INTO `$table` (`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        }
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    /**
     * Insert 插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param Boolean $debug
     * @return Int
     */
    public function insert($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "INSERT INTO `$table` (`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
//        $this->dbh->query($strSql);
        $this->getPDOError();
        return $this->dbh->lastInsertId();
    }
    /**
     * Replace 覆盖方式插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param Boolean $debug
     * @return Int
     */
    public function replace($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "REPLACE INTO `$table`(`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    /**
     * Delete 删除
     *
     * @param String $table 表名
     * @param String $where 条件
     * @param Boolean $debug
     * @return Int
     */
    public function delete($table, $where = '', $debug = false)
    {
        if ($where == '') {
            $this->outputError("'WHERE' is Null");
        } else {
            $strSql = "DELETE FROM `$table` WHERE $where";
            if ($debug === true) $this->debug($strSql);
            $result = $this->dbh->exec($strSql);
            $this->getPDOError();
            return $result;
        }
    }
    /**
     * execSql 执行SQL语句
     *
     * @param String $strSql
     * @param Boolean $debug
     * @return Int
     */
    public function execSql($strSql, $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    /**
     * 获取字段最大值
     *
     * @param string $table 表名
     * @param string $field_name 字段名
     * @param string $where 条件
     */
    public function getMaxValue($table, $field_name, $where = '', $debug = false)
    {
        $strSql = "SELECT MAX(".$field_name.") AS MAX_VALUE FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        $maxValue = $arrTemp["MAX_VALUE"];
        if ($maxValue == "" || $maxValue == null) {
            $maxValue = 0;
        }
        return $maxValue;
    }
    /**
     * 获取指定列的数量
     *
     * @param string $table
     * @param string $field_name
     * @param string $where
     * @param bool $debug
     * @return int
     */
    public function getCount($table, $field_name, $where = '', $debug = false)
    {
        $strSql = "SELECT COUNT($field_name) AS NUM FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        return $arrTemp['NUM'];
    }
    /**
     * 获取表引
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 更新数据库中表的统计信息
  • MySQL 存储过程中执行动态 SQL 语句
  • 金额大写转小写SQL
  • 财政年度表之建表约束
  • mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法
  • 统计数据库每天的数据增长量
  • MySql批量插入性能优化
  • 各大数据库分段查询技术的实现方式
  • SQL 循环插入1000条数据
  • 删除SQL 某个表中重复的记录

相关文章

  • 2018-12-05SQL Server 总结复习 (二)
  • 2017-09-17Heartbeat+DRBD+MySQL高可用方案
  • 2017-05-11MySQL索引使用全程分析
  • 2017-05-11mysql sharding(碎片)介绍
  • 2017-05-11mysql如何处理varchar与nvarchar类型中的特殊字符
  • 2018-12-05我的服务器SQL2000的sqlserver占用了90%的cpu,怎么查是那个库?
  • 2018-12-05关于目标数据的详细介绍
  • 2017-05-11mysql myisam 优化设置设置
  • 2018-12-05如何使用Spring boot操作mysql数据库
  • 2017-05-11MySQL数据库InnoDB引擎主从复制同步经验总结

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • 有关存储信息的文章推荐10篇
    • mysql应用中常用到的优化
    • MYSQL学习总结(四):MYSQL的恢复和备份
    • MySQL分布式集群之MyCAT的配置文件schema.xml详解
    • oracle下巧用bulk collect实现cursor批量fetch的sql语句
    • 理解SQL SERVER中的逻辑读,预读和物理读
    • SqlServer中批量替换被插入的木马记录
    • SQLServer2005安装提示服务无法启动原因分析及解决
    • sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务
    • 详解mysql列转行,合并字段的方法(图文)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有