標籤:php mysqli
<?phpclass MysqliCommon { private $mysqli; //內部建立mysqli對象 private $dbErrorFile = ‘‘; //錯誤記錄檔檔案 /** * * @param array $dbConfi * 類似於array("host"=>"127.0.0.1","dbname"=>"dilicms2","user"=>"root","password"=>"123qwe") * @param type $dbErrorFile */ public function __construct($dbConfi, $dbErrorFile = ‘‘, $charSet = ‘utf8‘) { $this->mysqli = @new mysqli($dbConfi[‘host‘], $dbConfi[‘user‘], $dbConfi[‘password‘], $dbConfi[‘dbname‘]); $this->dbErrorFile = $dbErrorFile; if ($this->mysqli->connect_errno) { $connectErrorMsg = date(‘Y-m-d H:i:s‘, time()) . ">>> Connection failed: " . mb_convert_encoding($this->mysqli->connect_error, ‘UTF-8‘, ‘GBK‘) . "\r\n"; $this->writeErrorLog($connectErrorMsg); exit(); } //設定訪問資料庫的字元集 $this->mysqli->query(‘set names ‘ . $charSet); } /** * 向錯誤記錄檔檔案中記錄錯誤記錄檔 * @param string $errorMsg */ public function writeErrorLog($errorMsg = ‘‘) { $this->dbErrorFile; if ($this->dbErrorFile != ‘‘) { $EMsg = ""; if ($errorMsg != ‘‘) { $EMsg = $errorMsg; } else { $EMsg = date(‘Y-m-d H:i:s‘, time()) . ">>> Connection failed: " . mb_convert_encoding($this->mysqli->error, ‘UTF-8‘, ‘GBK‘) . "\r\n"; } file_put_contents($this->dbErrorFile, $EMsg, FILE_APPEND); } } /** * 根據表名 欄位 條件 選項 查詢資料庫資料 * @param string $table //表名 * @param array $fields //欄位數組 * @param array $wheres //條件數組 * @param array $options //可選資料群組 */ public function seach($table, $fields, $wheres, $options = array()) { $sql = ‘SELECT ‘ . implode($fields, ", ") . ‘ FROM ‘ . $table . ‘ WHERE 1=1 ‘; $wheresSql = $this->dealWhere($wheres); $sql = $sql . $wheresSql; if (isset($options[‘limit‘])) { $sql.=‘ LIMIT ‘ . $options[‘limit‘]; } return $this->run($sql); } /** * 執行SQl語句 * @param string $sql * @return mixed */ private function run($sql) { $result = $this->mysqli->query($sql); if (!$result) { $this->writeErrorLog(); return false; } if (preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $sql)) { $resultArray = array(); if ($result->num_rows > 0) { while ($row = $result->fetch_array()) { $resultArray[] = $row; } } $result->free(); return $resultArray; } elseif (preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $sql)) { return $this->mysqli->affected_rows; } return false; } /** * 處理查詢條件 * @param array $wheres * @return string */ public function dealWhere($wheres) { $wheresSql = ‘‘; foreach ($wheres as $key => $value) { $cond = preg_replace("/\s+/", " ", $key); $fieldWh = explode(‘ ‘, trim($cond)); if (count($fieldWh) == 1) { $wheresSql .=‘ AND ‘ . $cond . ‘=‘ . (is_string($value) ? ‘ "‘ . $value . ‘" ‘ : $value . ‘ ‘); } else if (count($fieldWh) == 2) { $conStr = trim(strtoupper($fieldWh[0])); if ($conStr == ‘OR‘) { $wheresSql .=‘ OR ‘ . $cond . ‘=‘ . (is_string($value) ? ‘ "‘ . $value . ‘" ‘ : $value . ‘ ‘); } else { $wheresSql.=‘ AND ‘ . $fieldWh[0] . ‘ ‘ . $fieldWh[1] . (is_string($value) ? ‘ "‘ . $value . ‘" ‘ : $value . ‘ ‘); } } else if (count($fieldWh) == 3) { $conStr = trim(strtoupper($fieldWh[0])); if ($conStr == ‘OR‘) { $wheresSql.=‘ OR ‘ . $fieldWh[0] . ‘ ‘ . $fieldWh[1] . (is_string($value) ? ‘ "‘ . $value . ‘" ‘ : $value . ‘ ‘); } } } return $wheresSql; } /** * 開啟事務 */ public function beginTransaction() { $this->mysqli->autocommit(false); } /** * 結束事務 * @return boolean */ public function complectTransaction() { if (!$this->mysqli->errno) { $this->mysqli->commit(); return true; } else { $this->mysqli->rollback(); $this->writeErrorLog(); } return false; } public function insert($table, $values) { $sql = "INSERT INTO " . $table . " "; $columns = ""; $columnsVal = ""; foreach ($values as $key => $value) { $columns .= $key . ‘,‘; $columnsVal.=(is_string($value) ? ‘ "‘ . $this->mysqli->real_escape_string($value) . ‘",‘ : $value . ‘,‘); } $sql = $sql . "(" . substr($columns, 0, strlen($columns) - 1) . ") VALUES (" . substr($columnsVal, 0, strlen($columnsVal) - 1) . ")"; return $this->run($sql); } public function doClose() { $this->mysqli->close(); unset($this->mysqli); } public function delete($table, $wheres) { $sql = "DELETE FROM " . $table . ‘ WHERE 1=1 ‘; $wheresSql = $this->dealWhere($wheres); $sql = $sql . $wheresSql; return $this->run($sql); } public function update($table, $values, $wheres) { $sql = "UPDATE " . $table . " SET "; $count = 0; foreach ($values as $key => $value) { if ($count > 0) { $sql .= ", "; } $count++; $sql .= $key . " = " . (is_string($value) ? ‘ "‘ . $this->mysqli->real_escape_string($value) . ‘" ‘ : $value . ‘ ‘); } $sql2 = ‘ WHERE 1=1 ‘; $wheresSql = $this->dealWhere($wheres); $sql2 = $sql2 . $wheresSql; $sql = $sql . $sql2; return $this->run($sql); }}
本文出自 “我的IT生涯” 部落格,請務必保留此出處http://quietnight.blog.51cto.com/7163892/1675674
php對mysqli的小封裝