国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 php教程 PHP源碼 php mysql pdo操作類

php mysql pdo操作類

May 23, 2016 am 08:39 AM

提供mysql的增、刪、改、查等操作 ??

?[代碼][PHP]代碼 ?? ??

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 . &#39;<br />&#39;;
  $errorfile = _ROOT . &#39;./dberrorlog.php&#39;;
  $sql = str_replace ( array (
    "\n",
    "\r",
    "\t",
    "  ",
    "  ",
    "  "
  ), array (
    " ",
    " ",
    " ",
    " ",
    " ",
    " "
  ), $sql );
  if (! file_exists ( $errorfile )) {
   $fp = file_put_contents ( $errorfile, "<?PHP exit(&#39;Access Denied&#39;); ?>\n" . $sql );
  } else {
   $fp = file_put_contents ( $errorfile, "\n" . $sql, FILE_APPEND );
  }
 }
  
 /**
  * 作用:運行錯誤信息
  * 返回:運行錯誤信息和SQL語句
  * 類型:字符
  */
 private function sqlError($message = &#39;&#39;, $info = &#39;&#39;, $sql = &#39;&#39;) {
   
  $html = &#39;&#39;;
  if ($message) {
   $html .=  $message;
  }
   
  if ($info) {
   $html .= &#39;SQLID: &#39; . $info ;
  }
  if ($sql) {
   $html .= &#39;ErrorSQL: &#39; . $sql;
  }
   
  throw new Exception($html);
 }
/**
 * *******************錯誤處理結(jié)束********************
 */
}

? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ?

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)