-
-
/**
- * PDO数据库
- * @copyright By GOOGLE
- */
- class pdo_db
- {
- /**
- * PDO实例
- * @var PDO
- */
- protected $_db;
- /**
- * PDO准备语句
- * @var PDOStatement
- */
- protected $_stmt;
- /**
- * 最后的SQL语句
- * @var string
- */
- protected $_sql;
- /**
- * 配置信息 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
- * @var array
- */
- protected $_config;
- /**
- * 构造函数
- * @param array $config
- */
- public function __construct($config)
- {
- $this->_config = $config;
- }
- /**
- * 连接数据库
- * @return void
- */
- public function connect()
- {
- $this->_db = new PDO($this->_config['dsn'], $this->_config['name'], $this->_config['password'], $this->_config['option']);
- //默认把结果序列化成stdClass
- // $this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
- $this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- //自己写代码捕获Exception
- $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- }
- /**
- * 断开连接
- * @return void
- */
- public function disConnect()
- {
- $this->_db = null;
- $this->_stmt = null;
- }
- /**
- * 执行sql,返回新加入的id
- * @param string $statement
- * @return string
- */
- public function exec($statement)
- {
- if ($this->_db->exec($statement)){
- $this->_sql = $statement;
- return $this->lastId();
- }
- $this->errorMessage();
- }
- /**
- * 查询sql
- * @param string $statement
- * @return pdo_db
- */
- public function query($statement)
- {
- $res = $this->_db->query($statement);
- if ($res){
- $this->_stmt = $res;
- $this->_sql = $statement;
- return $this;
- }
- $this->errorMessage();
- }
- /**
- * 序列化一次数据
- * @return mixed
- */
- public function fetchOne()
- {
- return $this->_stmt->fetch();
- }
- /**
- * 序列化所有数据
- * @return array
- */
- public function fetchAll()
- {
- return $this->_stmt->fetchAll();
- }
- /**
- * 最后添加的id
- * @return string
- */
- public function lastId()
- {
- return $this->_db->lastInsertId();
- }
- /**
- * 影响的行数
- * @return int
- */
- public function affectRows()
- {
- return $this->_stmt->rowCount();
- }
- /**
- * 预备语句
- * @param string $statement
- * @return pdo_db
- */
- public function prepare($statement)
- {
- $res = $this->_db->prepare($statement);
- if ($res){
- $this->_stmt = $res;
- $this->_sql = $statement;
- return $this;
- }
- $this->errorMessage();
- }
- /**
- * 绑定数据
- * @param array $array
- * @return pdo_db
- */
- public function bindArray($array)
- {
- foreach ($array as $k => $v){
- if (is_array($v)){
- //array的有效结构 array('value'=>xxx,'type'=>PDO::PARAM_XXX)
- $this->_stmt->bindValue($k + 1, $v['value'], $v['type']);
- } else{
- $this->_stmt->bindValue($k + 1, $v, PDO::PARAM_STR);
- }
- }
- return $this;
- }
- /**
- * 执行预备语句
- * @return bool
- */
- public function execute()
- {
- if ($this->_stmt->execute()){
- return true;
- }
- $this->errorMessage();
- }
- /**
- * 开启事务
- * @return bool
- */
- public function beginTransaction()
- {
- return $this->_db->beginTransaction();
- }
- /**
- * 执行事务
- * @return bool
- */
- public function commitTransaction()
- {
- return $this->_db->commit();
- }
- /**
- * 回滚事务
- * @return bool
- */
- public function rollbackTransaction()
- {
- return $this->_db->rollBack();
- }
- /**
- * 抛出错误
- * @throws Error
- * @return void
- */
- public function errorMessage()
- {
- return;
- $msg = $this->_db->errorInfo();
- throw new Exception('数据库错误:' . $msg[2]);
- }
- //---------------------
- /**
- * 单例实例
- * @var pdo_db
- */
- protected static $_instance;
- /**
- * 默认数据库
- * @static
- * @param array $config
- * @return pdo_db
- */
- public static function instance($config)
- {
- if (!self::$_instance instanceof pdo_db){
- self::$_instance = new pdo_db($config);
- self::$_instance->connect();
- }
- return self::$_instance;
- }
- //----------------------
- /**
- * 获取PDO支持的数据库
- * @static
- * @return array
- */
- public static function getSupportDriver(){
- return PDO::getAvailableDrivers();
- }
- /**
- * 获取数据库的版本信息
- * @return array
- */
- public function getDriverVersion(){
- $name = $this->_db->getAttribute(PDO::ATTR_DRIVER_NAME);
- return array($name=>$this->_db->getAttribute(PDO::ATTR_CLIENT_VERSION));
- }
- }
复制代码
|
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31