操作類
class pdomysql { public static $dbtype = 'mysql'; public static $dbhost = ''; public static $dbport = ''; public static $dbname = ''; public static $dbuser = ''; public static $dbpass = ''; public static $charset = ''; public static $stmt = null; public static $DB = null; public static $connect = true; // 是否長(zhǎng)連接 public static $debug = false; private static $parms = array (); /** * 構(gòu)造函數(shù) */ public function __construct() { self::$dbtype = 'mysql'; self::$dbhost = HOST; self::$dbport = '3306'; self::$dbname = 'tion'; self::$dbuser = 'manager'; self::$dbpass = '123'; self::$connect = true; self::$charset = 'UTF8'; self::connect (); self::$DB->setAttribute ( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true ); self::$DB->setAttribute ( PDO::ATTR_EMULATE_PREPARES, true ); self::execute ( 'SET NAMES ' . self::$charset ); } /** * 析構(gòu)函數(shù) */ public function __destruct() { self::close (); } /** * *******************基本方法開(kāi)始******************** */ /** * 作用:連結(jié)數(shù)據(jù)庫(kù) */ public function connect() { try { self::$DB = new PDO ( self::$dbtype . ':host=' . self::$dbhost . ';port=' . self::$dbport . ';dbname=' . self::$dbname, self::$dbuser, self::$dbpass, array ( PDO::ATTR_PERSISTENT => self::$connect ) ); } catch ( PDOException $e ) { die ( "Connect Error Infomation:" . $e->getMessage () ); } } /** * 關(guān)閉數(shù)據(jù)連接 */ public function close() { self::$DB = null; } /** * 對(duì)字串進(jìn)行轉(zhuǎn)義 */ public function quote($str) { return self::$DB->quote ( $str ); } /** * 作用:獲取數(shù)據(jù)表里的欄位 * 返回:表字段結(jié)構(gòu) * 類型:數(shù)組 */ public function getFields($table) { self::$stmt = self::$DB->query ( "DESCRIBE $table" ); $result = self::$stmt->fetchAll ( PDO::FETCH_ASSOC ); self::$stmt = null; return $result; } /** * 作用:獲得最后INSERT的主鍵ID * 返回:最后INSERT的主鍵ID * 類型:數(shù)字 */ public function getLastId() { return self::$DB->lastInsertId (); } /** * 作用:執(zhí)行INSERT\UPDATE\DELETE * 返回:執(zhí)行語(yǔ)句影響行數(shù) * 類型:數(shù)字 */ public function execute($sql) { self::getPDOError ( $sql ); return self::$DB->exec ( $sql ); } /** * 獲取要操作的數(shù)據(jù) * 返回:合併后的SQL語(yǔ)句 * 類型:字串 */ private function getCode($table, $args) { $code = ''; if (is_array ( $args )) { foreach ( $args as $k => $v ) { if ($v == '') { continue; } $code .= "`$k`='$v',"; } } $code = substr ( $code, 0, - 1 ); return $code; } public function optimizeTable($table) { $sql = "OPTIMIZE TABLE $table"; self::execute ( $sql ); } /** * 執(zhí)行具體SQL操作 * 返回:運(yùn)行結(jié)果 * 類型:數(shù)組 */ private function _fetch($sql, $type) { $result = array (); self::$stmt = self::$DB->query ( $sql ); self::getPDOError ( $sql ); self::$stmt->setFetchMode ( PDO::FETCH_ASSOC ); switch ($type) { case '0' : $result = self::$stmt->fetch (); break; case '1' : $result = self::$stmt->fetchAll (); break; case '2' : $result = self::$stmt->rowCount (); break; } self::$stmt = null; return $result; } /** * *******************基本方法結(jié)束******************** */ /** * *******************Sql操作方法開(kāi)始******************** */ /** * 作用:插入數(shù)據(jù) * 返回:表內(nèi)記錄 * 類型:數(shù)組 * 參數(shù):$db->insert('$table',array('title'=>'Zxsv')) */ public function add($table, $args) { $sql = "INSERT INTO `$table` SET "; $code = self::getCode ( $table, $args ); $sql .= $code; return self::execute ( $sql ); } /** * 修改數(shù)據(jù) * 返回:記錄數(shù) * 類型:數(shù)字 * 參數(shù):$db->update($table,array('title'=>'Zxsv'),array('id'=>'1'),$where * ='id=3'); */ public function update($table, $args, $where) { $code = self::getCode ( $table, $args ); $sql = "UPDATE `$table` SET "; $sql .= $code; $sql .= " Where $where"; return self::execute ( $sql ); } /** * 作用:刪除數(shù)據(jù) * 返回:表內(nèi)記錄 * 類型:數(shù)組 * 參數(shù):$db->delete($table,$condition = null,$where ='id=3') */ public function delete($table, $where) { $sql = "DELETE FROM `$table` Where $where"; return self::execute ( $sql ); } /** * 作用:獲取單行數(shù)據(jù) * 返回:表內(nèi)第一條記錄 * 類型:數(shù)組 * 參數(shù):$db->fetOne($table,$condition = null,$field = '*',$where ='') */ public function fetOne($table, $field = '*', $where = false) { $sql = "SELECT {$field} FROM `{$table}`"; $sql .= ($where) ? " WHERE $where" : ''; return self::_fetch ( $sql, $type = '0' ); } /** * 作用:獲取所有數(shù)據(jù) * 返回:表內(nèi)記錄 * 類型:二維數(shù)組 * 參數(shù):$db->fetAll('$table',$condition = '',$field = '*',$orderby = '',$limit * = '',$where='') */ public function fetAll($table, $field = '*', $orderby = false, $where = false) { $sql = "SELECT {$field} FROM `{$table}`"; $sql .= ($where) ? " WHERE $where" : ''; $sql .= ($orderby) ? " ORDER BY $orderby" : ''; return self::_fetch ( $sql, $type = '1' ); } /** * 作用:獲取單行數(shù)據(jù) * 返回:表內(nèi)第一條記錄 * 類型:數(shù)組 * 參數(shù):select * from table where id='1' */ public function getOne($sql) { return self::_fetch ( $sql, $type = '0' ); } /** * 作用:獲取所有數(shù)據(jù) * 返回:表內(nèi)記錄 * 類型:二維數(shù)組 * 參數(shù):select * from table */ public function getAll($sql) { return self::_fetch ( $sql, $type = '1' ); } /** * 作用:獲取首行首列數(shù)據(jù) * 返回:首行首列欄位值 * 類型:值 * 參數(shù):select `a` from table where id='1' */ public function scalar($sql, $fieldname) { $row = self::_fetch ( $sql, $type = '0' ); return $row [$fieldname]; } /** * 獲取記錄總數(shù) * 返回:記錄數(shù) * 類型:數(shù)字 * 參數(shù):$db->fetRow('$table',$condition = '',$where =''); */ public function fetRowCount($table, $field = '*', $where = false) { $sql = "SELECT COUNT({$field}) AS num FROM $table"; $sql .= ($where) ? " WHERE $where" : ''; return self::_fetch ( $sql, $type = '0' ); } /** * 獲取記錄總數(shù) * 返回:記錄數(shù) * 類型:數(shù)字 * 參數(shù):select count(*) from table */ public function getRowCount($sql) { return self::_fetch ( $sql, $type = '2' ); } /** * *******************Sql操作方法結(jié)束******************** */ /** * *******************錯(cuò)誤處理開(kāi)始******************** */ /** * 設(shè)置是否為調(diào)試模式 */ public function setDebugMode($mode = true) { return ($mode == true) ? self::$debug = true : self::$debug = false; } /** * 捕獲PDO錯(cuò)誤信息 * 返回:出錯(cuò)信息 * 類型:字串 */ private function getPDOError($sql) { self::$debug ? self::errorfile ( $sql ) : ''; if (self::$DB->errorCode () != '00000') { $info = (self::$stmt) ? self::$stmt->errorInfo () : self::$DB->errorInfo (); echo (self::sqlError ( 'mySQL Query Error', $info [2], $sql )); exit (); } } private function getSTMTError($sql) { self::$debug ? self::errorfile ( $sql ) : ''; if (self::$stmt->errorCode () != '00000') { $info = (self::$stmt) ? self::$stmt->errorInfo () : self::$DB->errorInfo (); echo (self::sqlError ( 'mySQL Query Error', $info [2], $sql )); exit (); } } /** * 寫入錯(cuò)誤日志 */ private function errorfile($sql) { echo $sql . '<br />'; $errorfile = _ROOT . './dberrorlog.php'; $sql = str_replace ( array ( "\n", "\r", "\t", " ", " ", " " ), array ( " ", " ", " ", " ", " ", " " ), $sql ); if (! file_exists ( $errorfile )) { $fp = file_put_contents ( $errorfile, "<?PHP exit('Access Denied'); ?>\n" . $sql ); } else { $fp = file_put_contents ( $errorfile, "\n" . $sql, FILE_APPEND ); } } /** * 作用:運(yùn)行錯(cuò)誤信息 * 返回:運(yùn)行錯(cuò)誤信息和SQL語(yǔ)句 * 類型:字符 */ private function sqlError($message = '', $info = '', $sql = '') { $html = ''; if ($message) { $html .= $message; } if ($info) { $html .= 'SQLID: ' . $info ; } if ($sql) { $html .= 'ErrorSQL: ' . $sql; } throw new Exception($html); } /** * *******************錯(cuò)誤處理結(jié)束******************** */ }
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
指南:恆星刀片保存文件位置/保存文件丟失/不保存
4 週前
By DDD
Oguri Cap Build Guide |漂亮的德比志
2 週前
By Jack chen
Agnes Tachyon Build Guide |漂亮的德比志
1 週前
By Jack chen
沙丘:覺(jué)醒 - 高級(jí)行星學(xué)家Quest演練
4 週前
By Jack chen
約會(huì)一切:德克和哈珀關(guān)係指南
4 週前
By Jack chen

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)
