2
0

db.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class db
  3. {
  4. private static $dbh;
  5. private static $error;
  6. private static $stmt;
  7. private static function connexion()
  8. {
  9. if (self::$dbh) {
  10. return;
  11. }
  12. $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
  13. $options = array(
  14. PDO::ATTR_PERSISTENT => true,
  15. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  16. );
  17. try {
  18. self::$dbh = new PDO($dsn, DB_USER, DB_PASS, $options);
  19. } catch (PDOException $e) {
  20. self::$error = $e->getMessage();
  21. throw new Exception(self::$error);
  22. }
  23. }
  24. public static function version()
  25. {
  26. self::connexion();
  27. return self::$dbh->getAttribute(PDO::ATTR_SERVER_VERSION);
  28. }
  29. public static function query(string $query)
  30. {
  31. self::connexion();
  32. self::$stmt = self::$dbh->prepare($query);
  33. }
  34. public static function bind(string $param, $value = null, $type = null)
  35. {
  36. if (is_null($type)) {
  37. switch (true) {
  38. case is_int($value):
  39. $type = PDO::PARAM_INT;
  40. break;
  41. case is_bool($value):
  42. $type = PDO::PARAM_BOOL;
  43. break;
  44. case is_null($value):
  45. $type = PDO::PARAM_NULL;
  46. break;
  47. default:
  48. $type = PDO::PARAM_STR;
  49. }
  50. }
  51. self::$stmt->bindValue($param, $value, $type);
  52. }
  53. public static function execute()
  54. {
  55. self::connexion();
  56. if (debug::isFile("sql")) {
  57. ob_start();
  58. $return = self::$stmt->execute();
  59. self::$stmt->debugDumpParams();
  60. $debugOutput = ob_get_clean();
  61. debug::logSession($debugOutput);
  62. return $return;
  63. } else {
  64. return self::$stmt->execute();
  65. }
  66. }
  67. public static function resultset()
  68. {
  69. self::execute();
  70. return self::$stmt->fetchAll(PDO::FETCH_ASSOC);
  71. }
  72. public static function single()
  73. {
  74. self::execute();
  75. return self::$stmt->fetch(PDO::FETCH_ASSOC);
  76. }
  77. public static function rowCount()
  78. {
  79. return self::$stmt->rowCount();
  80. }
  81. public static function lastInsertId()
  82. {
  83. self::connexion();
  84. return self::$dbh->lastInsertId();
  85. }
  86. public static function beginTransaction()
  87. {
  88. self::connexion();
  89. return self::$dbh->beginTransaction();
  90. }
  91. public static function endTransaction()
  92. {
  93. self::connexion();
  94. return self::$dbh->commit();
  95. }
  96. public static function cancelTransaction()
  97. {
  98. self::connexion();
  99. return self::$dbh->rollBack();
  100. }
  101. public static function debugDumpParams()
  102. {
  103. self::connexion();
  104. return self::$stmt->debugDumpParams();
  105. }
  106. public static function queryError()
  107. {
  108. self::connexion();
  109. $qError = self::$dbh->errorInfo();
  110. if (!is_null($qError[2])) {
  111. echo $qError[2];
  112. }
  113. }
  114. public static function getError()
  115. {
  116. return self::$error;
  117. }
  118. }