您好,欢迎访问一九零五行业门户网

PHP的PDO数据库操作类

一个简单的pdo类封装。。仅供学习交流
pdodb 数据库类
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); //自己写代码捕获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 pdodb */ 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 pdodb */ 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 pdodb */ 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() { $msg = $this->db->errorinfo(); throw new error('数据库错误:' . $msg[2]); } //--------------------- /** * 单例实例 * @var pdodb */ protected static $_instance; /** * 默认数据库 * @static * @param array $config * @return pdodb */ public static function instance($config) { if (!self::$_instance instanceof pdodb) { self::$_instance = new pdodb($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)); } }
其它类似信息

推荐信息