平時項目中會用到對MySQL數據庫的操作,在此封裝一個操作類,方便以後擴展和使用。
class PDOmysql
{
private $host = "";//連接地址
private $user = "";//用戶名
private $pass = "";//密碼
public $dbcharset = "utf8";//默認的數據庫編碼
protected static $_instance = null;//數據庫實例
protected $dsn;
public $dbh = null;
/**
* 構造
* @param $dbname 數據庫名
* @param array $dbconf 數據庫連接信息
* @throws Exception
*/
private function __construct($dbname, $dbconf = array())
{
$host = isset($dbconf['host']) ? $dbconf['host'] : $this->host;
$user = isset($dbconf['user']) ? $dbconf['user'] : $this->user;
$pass = isset($dbconf['pass']) ? $dbconf['pass'] : $this->pass;
try {
$this->dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;
$this->dbh = new PDO($this->dsn, $user, $pass);
$this->dbh->exec('SET character_set_connection=' . $this->dbcharset . ', character_set_results=' . $this->dbcharset . ', character_set_client=binary');
} catch (PDOException $e) {
$this->outputError($e->getMessage());
}
}
/**
* Singleton instance
*
* @param $dbname
* @param array $dbconf 數據庫連接信息
* @return null|PDOmysql 存放數據庫實例
* @throws Exception
*/
public static function getInstance($dbname, $dbconf = array())
{
if (!self::$_instance) {
self::$_instance = new self($dbname, $dbconf);
}
return self::$_instance;
}
/**
* Query 查詢
*
* @param String $strSql SQL語句
* @param String $queryMode 查詢方式(All or Row)
* @param int $pdoMode 指定數據獲取方式
* @param Boolean $debug
* @return Array
*/
public function query($strSql, $queryMode = 'All', $pdoMode = PDO::FETCH_ASSOC, $debug = false)
{
if ($debug === true) $this->debug($strSql);
$recordset = $this->dbh->query($strSql);
$this->getPDOError();
if ($recordset) {
$recordset->setFetchMode($pdoMode);
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
* @throws Exception
*/
public function update($table, $arrayDataValue, $where = '', $debug = false)
{
// print_r($arrayDataValue);
$this->checkFields($table, $arrayDataValue);
if ($where) {
$strSql = '';
foreach ($arrayDataValue as $key => $value) {
if (!empty($value)) {
$strSql .= ", $key='$value'";
} else {
$strSql .= ", $key=null";
}
}
$strSql = substr($strSql, 1);
$strSql = "UPDATE $table SET $strSql WHERE $where";
// echo $strSql;
} 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
* @throws Exception
*/
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->getPDOError();
return $result;
}
/**
* Replace 覆蓋方式插入
*
* @param String $table 表名
* @param Array $arrayDataValue 字段與值
* @param Boolean $debug
* @return Int
* @throws Exception
*/
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
* @throws Exception
*/
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;
}
/**
* checkFields 檢查指定字段是否在指定數據表中存在
*
* @param String $table
* @param $arrayFields
* @throws Exception
*/
private function checkFields($table, $arrayFields)
{
$fields = $this->getFields($table);
foreach ($arrayFields as $key => $value) {
if (!in_array($key, $fields)) {
$this->outputError("Unknown column `$key` in field list.");
}
}
}
/**
* getFields 獲取指定數據表中的全部字段名
*
* @param String $table 表名
* @return array
*/
private function getFields($table)
{
$fields = array();
$recordset = $this->dbh->query("SHOW COLUMNS FROM $table");
$this->getPDOError();
$recordset->setFetchMode(PDO::FETCH_ASSOC);
$result = $recordset->fetchAll();
foreach ($result as $rows) {
$fields[] = $rows['Field'];
}
return $fields;
}
/**
* 獲取字段最大值
*
* @param string $table 表名
* @param string $field_name 字段名
* @param string $where 條件
* @param bool $debug
* @return int|mixed
*/
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 $where
* @param bool $debug
* @return int
*/
public function totalCount($table, $where = '', $debug = false)
{
$strSql = "SELECT COUNT(1) AS c FROM $table";
if ($where != '') $strSql .= " WHERE $where";
if ($debug === true) $this->debug($strSql);
$arrTemp = $this->query($strSql, 'Row');
return $arrTemp['c'];
}
/**
* 獲取表引擎
*
* @param String $dbName 庫名
* @param String $tableName 表名
* @return String
*/
public function getTableEngine($dbName, $tableName)
{
$strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='" . $tableName . "'";
$arrayTableInfo = $this->query($strSql);
$this->getPDOError();
return $arrayTableInfo[0]['Engine'];
}
/**
* beginTransaction 事務開始
*/
public function beginTransaction()
{
$this->dbh->beginTransaction();
}
/**
* commit 事務提交
*/
public function commit()
{
$this->dbh->commit();
}
/**
* rollback 事務回滾
*/
public function rollback()
{
$this->dbh->rollback();
}
/**
* transaction 通過事務處理多條SQL語句
* 調用前需通過getTableEngine判斷表引擎是否支持事務
*
* @param array $arraySql
* @return Boolean
*/
public function execTransaction($arraySql)
{
$retval = 1;
$this->beginTransaction();
foreach ($arraySql as $strSql) {
if ($this->execSql($strSql) == 0) $retval = 0;
}
if ($retval == 0) {
$this->rollback();
return false;
} else {
$this->commit();
return true;
}
}
/**
* getPDOError 捕獲PDO錯誤信息
*/
private function getPDOError()
{
if ($this->dbh->errorCode() != '00000') {
$arrayError = $this->dbh->errorInfo();
$this->outputError($arrayError[2]);
}
}
/**
* debug
*
* @param mixed $debuginfo
*/
private function debug($debuginfo)
{
var_dump($debuginfo);
exit();
}
/**
* 輸出錯誤信息
*
* @param String $strErrMsg
* @throws Exception
*/
private function outputError($strErrMsg)
{
throw new Exception('MySQL Error: ' . $strErrMsg);
}
/**
* 防止克隆
*
*/
private function __clone()
{
}
/**
* destruct 關閉數據庫連接
*/
public function __destruct()
{
$this->dbh = null;
}
}