123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
-
-
-
-
-
-
- class PDOStatementCommon {
-
-
-
- private $pdoStatement;
-
-
-
- public function __construct($obj) {
- $this->pdoStatement = $obj;
- }
-
-
-
- public function numRows() {
-
- return $this->pdoStatement->rowCount();
- }
-
-
-
- public function fetch($fetch_style = PDO::FETCH_ASSOC) {
- return $this->pdoStatement->fetch($fetch_style);
- }
-
-
-
- public function fetchRow($fetch_style = PDO::FETCH_ASSOC) {
- $row = $this->pdoStatement->fetch($fetch_style);
- return $row;
- }
-
- }
-
-
- class PDOCommon extends PDO {
-
-
-
- private $limit = 0;
-
-
-
- private $from = 0;
-
-
-
- public function __construct($dsn, $username = '', $password = '', $driver_options = array(), $isQuiet = false) {
- try {
- parent::__construct($dsn, $username, $password, $driver_options);
- } catch (Exception $e) {
- error_log($e->getMessage());
- if ($isQuiet) {
- die();
- } else {
- die("Unable to connect to the database server. " .
- "Please report the problem to an Administrator.");
- }
- }
- $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
-
-
- if ($this->getAttribute(PDO::ATTR_DRIVER_NAME) == "mysql") {
- $this->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, false);
- }
- }
-
-
-
- public function query($str) {
-
- if (!empty($this->limit)) {
- $str .= " LIMIT " . $this->limit;
- if (!empty($this->from)) {
- $str .= " OFFSET " . $this->from;
- }
-
-
-
- $this->limit = 0;
- $this->from = 0;
- }
-
- try {
- $obj_pdoStatement = parent::query($str);
- } catch (Exception $e) {
- error_log("[* SQL ERROR MESSAGE FOLLOWS:\n" .
- $e->getTraceAsString() .
- "\n" . $e->getMessage() .
- "\nFull SQL Statement:" . $str .
- "\n*]");
- die("<b>An error occurred while executing the SQL statement. " .
- "Please contact an Administrator and report the problem.</b>" .
- "<br /><hr />The following query generated an error:<br />" .
- "<pre>" .
- $this->formatSQLforHTML($str) .
- "\n\n" . $e->getMessage() .
- "</pre>");
- }
-
- $obj_pdoStatementCommon = new PDOStatementCommon($obj_pdoStatement);
-
- return $obj_pdoStatementCommon;
- }
-
-
-
- protected function formatSQLforHTML($str) {
- $Keyword = array("SELECT ", "WHERE ", " ON ", "AND ", "OR ",
- "FROM ", "LIMIT ", "UNION ",
- "INNER ", "LEFT ", "RIGHT ", "JOIN ", ",",
- "GROUP BY ", "ORDER BY ", "HAVING ");
- foreach ($Keyword as $key => $value) {
- if ($value == ",") {
- $Replace[$key] = "<b>" . $value . "</b>\n";
- } else {
- $Replace[$key] = "\n<b>" . $value . "</b>";
- }
- }
-
- return str_replace($Keyword, $Replace, $str);
- }
-
-
-
- public function queryOne($str) {
- $result = $this->query($str);
- $row = $result->fetch(PDO::FETCH_NUM);
-
- return $row[0];
- }
-
-
-
- public function queryRow($str) {
- $obj_pdoStatement = parent::query($str);
- return $obj_pdoStatement->fetch(PDO::FETCH_ASSOC);
- }
-
-
-
- public function setLimit($limit, $from = 0) {
- $this->limit = $limit;
- $this->from = $from;
- }
-
-
-
- public function escape($str) {
- return $this->quote($str);
- }
-
- }
|