Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Database.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace SCF\Core;
  3. use PDO;
  4. use PDOException;
  5. class Database {
  6. /**
  7. * @var null|PDO
  8. */
  9. private $db = NULL;
  10. /**
  11. * @var
  12. */
  13. private $stmt;
  14. /**
  15. * @var string
  16. */
  17. private $error;
  18. /**
  19. * Database constructor.
  20. * @param $dbext
  21. * @param $dbhost
  22. * @param $dbuser
  23. * @param $dbpass
  24. * @param $dbname
  25. */
  26. public function __construct($dbext, $dbhost, $dbuser, $dbpass, $dbname)
  27. {
  28. $dsn = $dbext.':dbname='.$dbname.';host='.$dbhost.';charset=utf8';
  29. $options = [
  30. PDO::ATTR_PERSISTENT => true,
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  32. ];
  33. try {
  34. $this->db = new PDO($dsn, $dbuser, $dbpass, $options);
  35. }
  36. catch (PDOException $e) {
  37. $this->error = $e->getMessage();
  38. }
  39. }
  40. /**
  41. * @param $query
  42. */
  43. public function query($query)
  44. {
  45. $this->stmt = $this->db->prepare($query);
  46. }
  47. /**
  48. * @param $param
  49. * @param $value
  50. * @param null $type
  51. */
  52. public function bind($param, $value, $type = null)
  53. {
  54. if(is_null($type)) {
  55. switch(true) {
  56. case is_int($value):
  57. $type = PDO::PARAM_INT;
  58. break;
  59. case is_bool($value):
  60. $type = PDO::PARAM_BOOL;
  61. break;
  62. case is_null($value):
  63. $type = PDO::PARAM_NULL;
  64. break;
  65. default:
  66. $type = PDO::PARAM_STR;
  67. }
  68. }
  69. $this->stmt->bindValue($param,$value,$type);
  70. }
  71. /**
  72. * @return mixed
  73. */
  74. public function execute()
  75. {
  76. return $this->stmt->execute();
  77. }
  78. /**
  79. * @return mixed
  80. */
  81. public function fetchArray()
  82. {
  83. $this->execute();
  84. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  85. }
  86. /**
  87. * @return mixed
  88. */
  89. public function single()
  90. {
  91. $this->execute();
  92. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  93. }
  94. /**
  95. * @return mixed
  96. */
  97. public function rowCount()
  98. {
  99. return $this->stmt->rowCount();
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function lastInsertId()
  105. {
  106. return $this->stmt->lastInsertId();
  107. }
  108. }