下面来给各位同这介绍一个mysql PDO 操作类例子,希望些文章对大家会带来帮助,文章是转朋友的自己没写.
介绍:1、只有在执行select、update、delete、insert等操作时才会连接数据库,2、采用PDO预处理方式,3、事务处理,4、错误输出.
php代码如下:
- <?php
-
-
-
-
-
-
-
-
-
- class MyMysql
- {
-
- static $dns = null;
-
-
- static $username = null;
-
-
- static $password = null;
-
-
- static $pdo = null;
-
-
- public $debug = null;
-
-
- private $_begin_transaction = false;
-
-
-
-
-
- public function __construct($debug = true, $database = 'default')
- {
- $this->debug = $debug;
- self::$dns = Yaf_Registry::get('config')->db->$database->dns;
- self::$username = Yaf_Registry::get('config')->db->$database->username;
- self::$password = Yaf_Registry::get('config')->db->$database->password;
- }
-
-
-
-
-
- static function instance()
- {
- if(is_null(self::$pdo))
- {
- try
- {
- self::$pdo = new PDO(self::$dns, self::$username, self::$password);
- self::$pdo->query('set names utf8');
- }
- catch(PDOException $e)
- {
- exit('PDOException: ' . $e->getMessage());
- }
- }
-
- return self::$pdo;
- }
-
-
-
-
-
-
-
-
- public function query($sql, $parameters = array(), $option = PDO::FETCH_ASSOC)
- {
- self::$pdo || self::instance();
-
- $stmt = self::$pdo->prepare($sql);
- $stmt->execute($parameters);
-
- $tmp = array();
- while($row = $stmt->fetch($option))
- {
- $tmp[] = $row;
- }
-
- if($this->debug)
- {
- $this->error($stmt);
- }
-
- return $tmp;
- }
-
-
-
-
-
-
-
- public function execute($sql, $parameters = array())
- {
- self::$pdo || self::instance();
-
- $stmt = self::$pdo->prepare($sql);
- $stmt->execute($parameters);
-
- if($this->debug)
- {
- $this->error($stmt);
- }
-
- return $stmt->rowCount();
- }
-
-
-
-
-
-
- public function exec($sql)
- {
- self::$pdo || self::instance();
-
- $rows = self::$pdo->exec($sql);
-
- if($this->debug)
- {
- $this->error();
- }
-
- return $rows;
- }
-
-
-
-
-
-
-
- public function insert($tableName, $data)
- {
- self::$pdo || self::instance();
-
- $fields = '`' . implode('`,`', array_keys($data)) . '`';
-
- $values = "'" . implode("','", $data) . "'";
-
- $sql = "INSERT INTO `{$tableName}`({$fields}) VALUES ({$values})";
-
- self::$pdo->exec($sql);
-
- if($this->debug)
- {
- $this->error();
- }
-
- return $this->getLastInsertId();
- }
-
-
-
-
-
-
-
- public function insertBatch($tableName, $data)
- {
- self::$pdo || self::instance();
-
- $fields = '`' . implode('`,`', array_keys($data[0])) . '`';
-
- $tmp = array();
- foreach($data as $value)
- {
- $tmp[] = "'" . implode("','", $value) . "'";
- }
-
- $values = "(" . implode("),(", $tmp) . ")";
-
- $sql = "INSERT INTO `{$tableName}`({$fields}) VALUES {$values}";
-
- $rows = self::$pdo->exec($sql);
-
- if($this->debug)
- {
- $this->error();
- }
-
- return $rows;
- }
-
-
-
-
-
-
-
-
- public function updateByPrimaryKey($tableName, $where, $data)
- {
- self::$pdo || self::instance();
-
-
- $whereId = array_keys($where);
- $whereValue = array_values($where);
-
- $tmp = array();
- foreach($data as $key => $value)
- {
- $tmp[] = "`{$key}`='{$value}'";
- }
-
- $data = implode(',', $tmp);
-
- $sql = "UPDATE `{$tableName}` SET {$data} WHERE `{$whereId[0]}`='{$whereValue[0]}'";
-
- $rows = self::$pdo->exec($sql);
-
- if($this->debug)
- {
- $this->error();
- }
-
- return $rows;
- }
-
-
-
-
-
-
-
- public function deleteByPrimaryKey($tableName, $where)
- {
- self::$pdo || self::instance();
-
-
- $whereId = array_keys($where);
- $whereValue = array_values($where);
-
- $sql = "DELETE FROM `{$tableName}` WHERE `{$whereId[0]}`='{$whereValue[0]}'";
-
- $rows = self::$pdo->exec($sql);
-
- if($this->debug)
- {
- $this->error();
- }
-
- return $rows;
- }
-
-
-
-
-
- public function getLastInsertId()
- {
- self::$pdo || self::instance();
-
- return self::$pdo->lastInsertId();
- }
-
-
-
-
- public function error($stmt = '')
- {
- $error = $stmt ? $stmt->errorInfo() : self::$pdo->errorInfo();
-
- $msg = "SQLSTATE:{$error[0]}";
-
- if($error[1])
- {
- $msg .= " - ERRORCODE:{$error[1]}";
- }
-
- if($error[2])
- {
- $msg .= " - ERROR:{$error[2]}";
- }
-
- if($error[1] || $error[2])
- {
- exit($msg);
- }
- }
-
-
-
-
-
- public function begin()
- {
- self::$pdo || self::instance();
-
-
- $this->rollback();
-
- if(!self::$pdo->beginTransaction())
- {
- return false;
- }
-
- return $this->_begin_transaction = true;
- }
-
-
-
-
-
- public function commit()
- {
- if($this->_begin_transaction)
- {
- $this->_begin_transaction = false;
- self::$pdo->commit();
- }
-
- return true;
- }
-
-
-
-
-
- public function rollback()
- {
- if($this->_begin_transaction)
- {
- $this->_begin_transaction = false;
- self::$pdo->rollback();
- }
-
- return false;
- }
-
-
-
-
- public function close()
- {
- self::$pdo = null;
- }
- }
- ?>
|