Created
July 17, 2011 09:49
-
-
Save fasthold/1087407 to your computer and use it in GitHub Desktop.
简易封装PDO_MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pdo MySql driver wrap | |
* | |
* @category Class | |
* @package Fasthold Own PHP Snippet | |
* @copyright Copyright (c) 2007 | |
* @license New BSD License | |
* @author Fasthold Lau ([email protected]) | |
*/ | |
/** | |
* @example: | |
* | |
* $db = new Pdo_Mysql(array('host'=>'localhost','username'=>'root','password'=>'','dbname'=>'uker_access')); | |
* $data = $db->fetchAll("select * from pe_article limit 10"); | |
* print_r($data); | |
* | |
* | |
*/ | |
class Pdo_Mysql { | |
protected $_params; | |
/** | |
* PDO Object | |
* | |
* @var PDO | |
*/ | |
private $_connection; | |
/** | |
* Initialize | |
* | |
* @param array $params | |
* @return PDO | |
*/ | |
public function __construct(array $params) { | |
$this->_params = $params; | |
$this->getConnection($this->_params); | |
} | |
/** | |
* Get the connection object | |
* | |
* @return PDO | |
*/ | |
public function getConnection() { | |
if($this->_connection === null) { | |
$connectionOptions = array(); | |
if(empty($this->_params['charset'])) { | |
$this->_params['charset'] = 'utf8'; | |
} | |
if(! $this->_connection = new PDO( | |
'mysql:host='.$this->_params['host'].';dbname='.$this->_params['dbname'].'', | |
$this->_params['username'], | |
$this->_params['password'], | |
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$this->_params['charset']}")) ) { | |
throw new PDOException("Can't connect to database"); | |
} | |
} | |
return $this->_connection; | |
} | |
/** | |
* Fetch data set | |
* | |
* @param string $sql | |
* @return array | |
*/ | |
public function fetchAll($sql) { | |
$stmt = $this->exec($sql); | |
if($stmt instanceof PDOStatement) { | |
return $stmt->fetchAll(); | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Fetch column data | |
* | |
* @param string $sql | |
* @return array | |
*/ | |
public function fetchColumn($sql) { | |
$stmt = $this->exec($sql); | |
return current($stmt->fetch()); | |
} | |
/** | |
* Fetch only one row data | |
* | |
* @param string $sql | |
* @return array | |
*/ | |
public function fetchRow($sql) { | |
$stmt = $this->exec($sql); | |
if(false === $stmt) { | |
return array(); | |
} | |
return $stmt->fetch(PDO::FETCH_ASSOC); | |
} | |
/** | |
* Execute a SQL statement | |
* | |
* @param string $sql | |
* @return PDOStatement | |
*/ | |
public function exec($sql) { | |
$stmt = $this->prepare($sql); | |
$stmt->execute(); | |
return $stmt; | |
} | |
/** | |
* Prepare statement | |
* | |
* @param string $sql | |
* @return PDOStatement | |
*/ | |
public function prepare($sql) { | |
$stmt = $this->_connection->prepare($sql); | |
return $stmt; | |
} | |
/** | |
* Quote string | |
* | |
* @param string $sql | |
* @return string | |
*/ | |
public function quote($str) { | |
return $this->_connection->quote($str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment