操作類
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; // 是否長連接 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 (); } /** * *******************基本方法開始******************** */ /** * 作用:連結(jié)數(shù)據(jù)庫 */ 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; } /** * 對字串進行轉(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í)行語句影響行數(shù) * 類型:數(shù)字 */ public function execute($sql) { self::getPDOError ( $sql ); return self::$DB->exec ( $sql ); } /** * 獲取要操作的數(shù)據(jù) * 返回:合併后的SQL語句 * 類型:字串 */ 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操作 * 返回:運行結(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操作方法開始******************** */ /** * 作用:插入數(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é)束******************** */ /** * *******************錯誤處理開始******************** */ /** * 設(shè)置是否為調(diào)試模式 */ public function setDebugMode($mode = true) { return ($mode == true) ? self::$debug = true : self::$debug = false; } /** * 捕獲PDO錯誤信息 * 返回:出錯信息 * 類型:字串 */ 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 (); } } /** * 寫入錯誤日志 */ 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 ); } } /** * 作用:運行錯誤信息 * 返回:運行錯誤信息和SQL語句 * 類型:字符 */ 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); } /** * *******************錯誤處理結(jié)束******************** */ }
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
1 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
3 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
