PDO(PHP Data Object)是啥?
PDO是PHP 5新加入的一個重大功能,因為在PHP 5以前的php4/php3都是一堆的數據庫擴展來跟各個數據庫的連接和處理,什麼 php_mysql.dll、php_pgsql.dll、php_mssql.dll、php_sqlite.dll等等擴展來連接MySQL、 PostgreSQL、MS SQL Server、SQLite,同樣的,我們必須借助 ADOdb、PEAR::DB、PHPlib::DB之類的數據庫抽象類來幫助我們,無比煩瑣和低效,畢竟,php代碼的效率怎麼能夠我們直接用C/C+ +寫的擴展斜率高捏?所以嘛,PDO的出現是必然的,大家要平靜學習的心態去接受使用,也許你會發現能夠減少你不少功夫哦。
下面列舉一支PDO的Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
<?php /* * Author: AthrunSoft * class.pdomysql.php 0.01 2012-4-15 */ 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(); /** * 構造函數 */ public function __construct() { 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); } /** *析構函數 */ public function __destruct() { self::close(); } /*********************基本方法開始*********************/ /** * 作用:連結資料庫 */ 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()); } } /** *關閉資料連接 */ public function close() { self::$DB = null; } /** * 對字串進行轉義 */ public function quote($str) { return self::$DB->quote($str); } /** * 返回一個日期時間字符 */ public function now() { return date("Y-m-d H:i:s"); } /** * 作用:獲取當前庫的所有表名 * 返回:當前庫的所有表名 * 類型:陣列 */ public function getTablesName() { self::$stmt = self::$DB->query('SHOW TABLES FROM ' . self::$dbname); $result = self::$stmt->fetchAll(PDO::FETCH_NUM); self::$stmt = null; return $result; } /** * 作用:獲取資料表裡的欄位 * 返回:表字段結構 * 類型:陣列 */ 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 * 類型:數字 */ public function getLastId() { return self::$DB->lastInsertId(); } /** *事務開始 */ public function autocommit() { self::$DB->beginTransaction(); } /** *事務提交 */ public function commit() { self::$DB->commit(); } /** *交易復原 */ public function rollback() { self::$DB->rollback(); } /** * 作用:執行INSERTUPDATEDELETE * 返回:執行語句影響行數 * 類型:數字 */ public function execute($sql) { self::getPDOError($sql); return self::$DB->exec($sql); } /** * 獲取要操作的資料 * 返回:合併後的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; } /** * 執行具體SQL操作 * 返回:運行結果 * 類型:陣列或數位 */ 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': if ($sql) { $result = self::$stmt->fetchColumn(); } elseif (self::$stmt) { $result = self::$stmt->rowCount(); } else { $result = 0; } break; } self::$stmt = null; return $result; } /** * 分頁計算 */ private function count($pagesize, &$page, &$pagecount, &$recordcount) { //獲取記錄行數,計算頁數,以及重新檢查當前頁碼 $pagecount = ceil($recordcount / $pagesize); if ($pagecount > 0) { if ($page > $pagecount) { $page = $pagecount; } elseif ($page < 1) { $page = 1; } } else { $page = 1; } } /*********************基本方法結束*********************/ /*********************Sql操作方法開始*********************/ /** * 作用:插入資料 * 返回:表內記錄 * 類型:陣列 * 參數:$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); } /** * 修改資料 * 返回:記錄數 * 類型:數字 * 參數:$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); } /** * 作用:刪除資料 * 返回:表內記錄 * 類型:陣列 * 參數:$db->delete($table,$condition = null,$where ='id=3') */ public function delete($table, $where) { $sql = "DELETE FROM `$table` Where $where"; return self::execute($sql); } /** * 作用:獲取單行資料 * 返回:表內第一條記錄 * 類型:陣列 * 參數:$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'); } /** * 作用:獲取單行資料 * 返回:表內第一條記錄 * 類型:陣列 * 參數:select * from table where id='1' */ public function getOne($sql) { return self::_fetch($sql, $type = '0'); } /** * 作用:獲取首行首列資料 * 返回:首行首列欄位值 * 類型:值 * 參數:select `a` from table where id='1' */ public function scalar($sql, $fieldname) { $row = self::_fetch($sql, $type = '0'); return $row[$fieldname]; } /** * 獲取記錄總數 * 返回:記錄數 * 類型:數字 * 參數:$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 = '2'); } /** * 獲取記錄總數 * 返回:記錄數 * 類型:數字 * 參數:select count(*) from table */ public function getRowCount($sql) { return self::_fetch($sql, $type = '2'); } /** * 作用:獲取所有資料 * 返回:表內記錄 * 類型:二維陣列 * 參數:$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'); } /** * 作用:獲取分頁資料 * 返回:表內記錄 * 類型:二維陣列 */ public function fetPageAll($table, $field = '*', $where = false, $orderby = false, $pagesize, &$page, &$pagecount, &$recordcount) { $sql = "SELECT {$field} FROM `{$table}`"; $sql .= ($where) ? " WHERE $where" : ''; return self::getPageAll($sql, $orderby, $pagesize, $page, $pagecount, $recordcount); } /** * 作用:獲取所有資料 * 返回:表內記錄 * 類型:二維陣列 * 參數:select * from table */ public function getAll($sql) { return self::_fetch($sql, $type = '1'); } /** * 作用:獲取分頁資料 * 返回:表內記錄 * 類型:二維陣列 */ public function getPageAll($sql, $orderby = false, $pagesize, &$page, &$pagecount, &$recordcount) { $sqlcount = "select count(1) as `recordcount` from ($sql) as t;"; $recordcount = self::scalar($sqlcount, 'recordcount'); self::count($pagesize, $page, $pagecount, $recordcount); $start = ($page - 1) * $pagesize; $sql .= ($orderby) ? " ORDER BY $orderby" : ''; $sql .= " limit $start,$pagesize"; return self::_fetch($sql, $type = '1'); } /*********************Sql操作方法結束*********************/ /*********************Pram操作方法開始*********************/ /** * 作用:獲取單行資料 * 返回:表內第一條記錄 * 類型:陣列 */ public function pramGetOne($sql, $input_parameters) { return self::_pramfetch($sql, $input_parameters, $type = '0'); } /** * 作用:獲取所有資料 * 返回:表內記錄 * 類型:二維陣列 */ public function pramGetAll($sql, $input_parameters) { return self::_pramfetch($sql, $input_parameters, $type = '1'); } /** * 作用:執行帶參數SQL操作 * 返回:執行語句影響行數 * 類型:數字 */ public function pramExecute($sql, $input_parameters) { return self::_pramfetch($sql, $input_parameters, $type = '2'); } /** * 作用:獲取首行首列資料 * 返回:首行首列欄位值 * 類型:值 */ public function pramScalar($sql, $input_parameters, $fieldname) { $row = self::_pramfetch($sql, $input_parameters, $type = '0'); return $row[$fieldname]; } /** * 執行帶參數SQL操作 * 返回:運行結果 * 類型:陣列或數位 */ private function _pramfetch($sql, $input_parameters, $type) { $result = array(); self::$stmt = self::$DB->prepare($sql); self::getPDOError($sql); self::$stmt->execute($input_parameters); self::getSTMTError($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': if (self::$stmt) { $result = self::$stmt->rowCount(); } else { $result = 0; } break; } self::$stmt = null; return $result; } /*********************Pram操作方法結束*********************/ /*********************Proc操作方法開始*********************/ /** * 添加參數 */ public function pramadd($parameter, $variable, $data_type, $length) { array_push(self::$parms, array($parameter, $variable, $data_type, $length)); } /** * 清除所有參數 */ public function pramclear() { self::$parms = array(); } /** * 作用:獲取單行資料 * 返回:表內第一條記錄 * 類型:陣列 */ public function procGetOne($sql) { return self::_procfetch($sql, $type = '0'); } /** * 作用:獲取所有資料 * 返回:表內記錄 * 類型:二維陣列 */ public function procGetAll($sql) { return self::_procfetch($sql, $type = '1'); } /** * 作用:執行一個存儲過程 * 返回:執行語句影響行數 * 類型:數字 */ public function procExecute($sql) { return self::_procfetch($sql, $type = '2'); } /** * 作用:獲取out型的return */ public function getReturn() { return self::scalar("select @ireturn AS iReturn", "iReturn"); } /** * 執行帶參數SQL操作 * 返回:運行結果 * 類型:陣列或數位 */ private function _procfetch($sql, $type) { $result = array(); self::$stmt = self::$DB->prepare($sql); self::getPDOError($sql); foreach (self::$parms as $pram) { self::$stmt->bindParam($pram[0], $pram[1], $pram[2], $pram[3]); } self::$stmt->execute(); self::getSTMTError($sql); self::$stmt->setFetchMode(PDO::FETCH_ASSOC); switch ($type) { case '0': $result = self::$stmt->fetch(); break; case '1': // do{ // $result[] = self::$stmt->fetchAll(); // } while (self::$stmt->nextRowset()); $result = self::$stmt->fetchAll(); self::$stmt->closeCursor(); break; case '2': if (self::$stmt) { $result = self::$stmt->rowCount(); } else { $result = 0; } break; } self::$stmt = null; return $result; } /** * 權為測試使用,目前沒有辦法獲取到OUT參數. * 另外在一存講過程中若有rowset,同時還有OUT參數時, * 即使$db->scalar("select @Rcount AS Rcount", "Rcount")這种方式仍然獲到不到, * 執行時會出現下面的錯誤: * Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. */ public function procExecOut() { //$colour = 'red'; self::$stmt = self::$DB->prepare('CALL puree_fruit(?)'); self::$stmt->bindParam(1, $colour,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000); self::$stmt->execute(); print("After pureeing fruit, the colour is: $colour"); } /*********************Proc操作方法結束*********************/ /*********************錯誤處理開始*********************/ /** * 設置是否為調試模式 */ 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 = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'; $html .= '<head><title>mySQL Message</title><style type="text/css">body {margin:0px;color:#555555;font-size:12px;background-color:#efefef;font-family:Verdana} ol {margin:0px;padding:0px;} .w {width:800px;margin:100px auto;padding:0px;border:1px solid #cccccc;background-color:#ffffff;} .h {padding:8px;background-color:#ffffcc;} li {height:auto;padding:5px;line-height:22px;border-top:1px solid #efefef;list-style:none;overflow:hidden;}</style></head>'; $html .= '<body><div class="w"><ol>'; if ($message) { $html .= '<div class="h">' . $message . '</div>'; } $html .= '<li>Date: ' . date('Y-n-j H:i:s', time()) . '</li>'; if ($info) { $html .= '<li>SQLID: ' . $info . '</li>'; } if ($sql) { $html .= '<li>Error: ' . $sql . '</li>'; } $html .= '</ol></div></body></html>'; return $html; } /*********************錯誤處理結束*********************/ } ?> |
其他參考資料: