2
0

db.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. return $return;
  62. } else {
  63. return self::$stmt->execute();
  64. }
  65. }
  66. public static function resultset()
  67. {
  68. self::execute();
  69. return self::$stmt->fetchAll(PDO::FETCH_ASSOC);
  70. }
  71. public static function single()
  72. {
  73. self::execute();
  74. return self::$stmt->fetch(PDO::FETCH_ASSOC);
  75. }
  76. public static function rowCount()
  77. {
  78. return self::$stmt->rowCount();
  79. }
  80. public static function lastInsertId()
  81. {
  82. self::connexion();
  83. return self::$dbh->lastInsertId();
  84. }
  85. public static function beginTransaction()
  86. {
  87. self::connexion();
  88. return self::$dbh->beginTransaction();
  89. }
  90. public static function endTransaction()
  91. {
  92. self::connexion();
  93. return self::$dbh->commit();
  94. }
  95. public static function cancelTransaction()
  96. {
  97. self::connexion();
  98. return self::$dbh->rollBack();
  99. }
  100. public static function debugDumpParams()
  101. {
  102. self::connexion();
  103. return self::$stmt->debugDumpParams();
  104. }
  105. public static function queryError()
  106. {
  107. self::connexion();
  108. $qError = self::$dbh->errorInfo();
  109. if (!is_null($qError[2])) {
  110. echo $qError[2];
  111. }
  112. }
  113. public static function getError()
  114. {
  115. return self::$error;
  116. }
  117. }