操作類
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; } /** * 對字串進(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í)行語句影響行數(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é)束******************** */ }
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undress AI Tool
Gambar buka pakaian secara percuma

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas
Panduan: bilah bintang simpan fail lokasi/simpan fail hilang/tidak menyimpan
4 minggu yang lalu
By DDD
Agnes Tachyon Build Guide | Musume Derby Pretty
1 minggu yang lalu
By Jack chen
Oguri Cap Build Guide | Musume Derby Pretty
2 minggu yang lalu
By Jack chen
Dune: Awakening - Lanjutan Planetologist Quest Walkthrough
3 minggu yang lalu
By Jack chen
Tarikh Segala -galanya: Panduan Hubungan Dirk dan Harper
4 minggu yang lalu
By Jack chen

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)
