Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. <?php
  2. if (!isset($_SESSION))
  3. session_start();
  4.  
  5. private function __construct() {
  6. mb_internal_encoding('UTF-8');
  7. mb_regex_encoding('UTF-8');
  8. mysqli_report(MYSQLI_REPORT_STRICT);
  9. $dsn = 'mysql:host=' . $this->host . '; dbname=' . $this->name . '; charset=utf8';
  10. $options = array(
  11. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  12. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  13. );
  14. try {
  15. $this->db = new PDO($dsn, $this->user, $this->pass, $options);
  16. } catch (PDOException $e) {
  17. exit('<p><strong>CONSTRUCT ERROR</strong></p>' . $e->getMessage());
  18. }
  19. $this->query("SET collation_connection = 'utf8mb4_general_ci'");
  20. $this->execute();
  21. }
  22. public function __destruct() {
  23. if (!$this->db)
  24. return null;
  25. $this->db = null;
  26. return null;
  27. }
  28. public function query($query) {
  29. $this->last_query = $query;
  30. $this->num_queries++;
  31. try {
  32. $this->stmt = $this->db->prepare($query);
  33. } catch (PDOException $e) {
  34. echo '<p><strong>QUERY ERROR</strong></p>' . $e->getMessage();
  35. error_log($e->getMessage() . ' - ' . $_SERVER['PHP_SELF'] . ' - ' . __FILE__, 0);
  36. exit;
  37. }
  38. }
  39. public function prepare($query) {
  40. try {
  41. $this->db->prepare($query);
  42. } catch (PDOException $e) {
  43. echo '<p><strong>QUERY (PREPARE) ERROR</strong></p>' . $e->getMessage();
  44. error_log($e->getMessage() . ' - ' . $_SERVER['PHP_SELF'] . ' - ' . __FILE__, 0);
  45. exit;
  46. }
  47. }
  48. public function bind($param, $value, $type = null) {
  49. if (is_null($type))
  50. switch (true) {
  51. case is_int($value):
  52. $type = PDO::PARAM_INT;
  53. break;
  54. case is_bool($value):
  55. $type = PDO::PARAM_BOOL;
  56. break;
  57. case is_null($value):
  58. $type = PDO::PARAM_NULL;
  59. break;
  60. default:
  61. $type = PDO::PARAM_STR;
  62. break;
  63. }
  64. try {
  65. $this->stmt->bindValue($param, $value, $type);
  66. } catch (PDOException $e) {
  67. exit('<p><strong>BIND ERROR</strong></p>' . $e->getMessage());
  68. }
  69. }
  70. public function execute(array $binds = null) {
  71. if (!isset($this->stmt))
  72. return false;
  73. try {
  74. if (count($binds) > 0)
  75. return $this->stmt->execute($binds);
  76. else
  77. return $this->stmt->execute();
  78. } catch (PDOException $e) {
  79. echo "<p><strong>EXECUTION ERROR</strong></p>" . $e->getMessage() . " " .$this->last_query;
  80. error_log($e->getMessage() . ' - ' . $_SERVER['PHP_SELF'] . ' - ' . __FILE__, 0);
  81. exit;
  82. }
  83. }
  84. public function fetch_row($shifted = false) {
  85. if (!isset($this->stmt))
  86. return null;
  87. try {
  88. $this->execute();
  89. $ret = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  90. if ($shifted === true)
  91. $ret = array_shift($ret);
  92. return $ret;
  93. } catch (PDOException $e) {
  94. exit('<p><strong>FETCH ROW ERROR</strong></p>' . $e->getMessage());
  95. }
  96. }
  97. public function fetch_single() {
  98. if (!isset($this->stmt))
  99. return null;
  100. try {
  101. $this->execute();
  102. return $this->stmt->fetchColumn(0);
  103. } catch (PDOException $e) {
  104. exit('<p><strong>FETCH SINGLE ERROR</strong></p>' . $e->getMessage());
  105. }
  106. }
  107. public function fetch_object() {
  108. if (!isset($this->stmt))
  109. return null;
  110. try {
  111. $this->execute();
  112. return $this->stmt->fetch(PDO::FETCH_OBJ);
  113. } catch (PDOException $e) {
  114. exit('<p><strong>FETCH OBJECT ERROR</strong></p>' . $e->getMessage());
  115. }
  116. }
  117. public function affected_rows() {
  118. try {
  119. return $this->stmt->rowCount();
  120. } catch (PDOException $e) {
  121. exit('<p><strong>AFFECTED ROWS ERROR</strong></p>' . $e->getMessage());
  122. }
  123. }
  124. public function num_rows() {
  125. try {
  126. return $this->stmt->fetchColumn();
  127. } catch (PDOException $e) {
  128. exit('<p><strong>NUM ROWS ERROR</strong></p>' . $e->getMessage());
  129. }
  130. }
  131. public function insert_id() {
  132. try {
  133. return $this->db->lastInsertId();
  134. } catch (PDOException $e) {
  135. exit('<p><strong>LAST INSERT ID ERROR</strong></p>' . $e->getMessage());
  136. }
  137. }
  138. public function query_error() {
  139. if (!isset($_SESSION['userid']))
  140. $_SESSION['userid'] = 0;
  141. if ($_SESSION['userid'] == 2)
  142. echo "<p><strong>QUERY ERROR:</strong> " . $this->error . "<br />Query was " . $this->last_query . "</p><br /><br />";
  143. exit("An error has been detected");
  144. }
  145. public function escape($str) {
  146. return $str;
  147. }
  148. public function tableExists($table) {
  149. try {
  150. $result = $this->db->query("SELECT 1 FROM `" . $table . "` LIMIT 1");
  151. } catch (Exception $e) {
  152. return false;
  153. }
  154. return $result !== false;
  155. }
  156. public function startTrans() {
  157. return $this->db->beginTransaction();
  158. }
  159. public function endTrans() {
  160. return $this->db->commit();
  161. }
  162. public function cancelTransaction() {
  163. return $this->db->rollBack();
  164. }
  165. public function error() {
  166. echo "<pre>";
  167. var_dump($this->stmt->debugDumpParams());
  168. echo "</pre>";
  169. }
  170. // Helper function(s)
  171. public function truncate(array $tables = null) {
  172. if (!count($tables))
  173. return false;
  174. $this->startTrans();
  175. foreach ($tables as $table) {
  176. $this->query('TRUNCATE TABLE ?');
  177. $this->execute(array($table));
  178. }
  179. $this->endTrans();
  180. }
  181. }
  182. $db = database::getInstance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement