Advertisement
Guest User

db.class.php

a guest
Aug 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <?php
  2. class db {
  3. private $conn;
  4. private $host;
  5. private $user;
  6. private $password;
  7. private $baseName;
  8. private $port;
  9. private $Debug;
  10.  
  11. function __construct($params=array()) {
  12. $this->conn = false;
  13. $this->host = 'localhost'; //hostname
  14. $this->user = ''; //username
  15. $this->password = ''; //password
  16. $this->baseName = 'testdb1'; //name of your database
  17. $this->port = '3306';
  18. $this->debug = true;
  19. $this->connect();
  20. }
  21.  
  22. function __destruct() {
  23. $this->disconnect();
  24. }
  25.  
  26. function connect() {
  27. if (!$this->conn) {
  28. try {
  29. $this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
  30. }
  31. catch (Exception $e) {
  32. die('Erreur : ' . $e->getMessage());
  33. }
  34.  
  35. if (!$this->conn) {
  36. $this->status_fatal = true;
  37. echo 'Connection BDD failed';
  38. die();
  39. }
  40. else {
  41. $this->status_fatal = false;
  42. }
  43. }
  44.  
  45. return $this->conn;
  46. }
  47.  
  48. function disconnect() {
  49. if ($this->conn) {
  50. $this->conn = null;
  51. }
  52. }
  53.  
  54. function getOne($query) {
  55. $result = $this->conn->prepare($query);
  56. $ret = $result->execute();
  57. if (!$ret) {
  58. echo 'PDO::errorInfo():';
  59. echo '<br />';
  60. echo 'error SQL: '.$query;
  61. die();
  62. }
  63. $result->setFetchMode(PDO::FETCH_ASSOC);
  64. $reponse = $result->fetch();
  65.  
  66. return $reponse;
  67. }
  68.  
  69. function getAll($query) {
  70. $result = $this->conn->prepare($query);
  71. $ret = $result->execute();
  72. if (!$ret) {
  73. echo 'PDO::errorInfo():';
  74. echo '<br />';
  75. echo 'error SQL: '.$query;
  76. die();
  77. }
  78. $result->setFetchMode(PDO::FETCH_ASSOC);
  79. $reponse = $result->fetchAll();
  80.  
  81. return $reponse;
  82. }
  83.  
  84. function pagi($query) //for pagination use with count in sql
  85. {
  86. $result = $this->conn->prepare($query);
  87. $ret = $result->execute();
  88. if (!$ret) {
  89. echo 'PDO::errorInfo():';
  90. echo '<br />';
  91. echo 'error SQL: '.$query;
  92. die();
  93. }
  94.  
  95. $response = $result->fetch();
  96. return $response;
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. function execute($query) {
  106. if (!$response = $this->conn->exec($query)) {
  107. echo 'PDO::errorInfo():';
  108. echo '<br />';
  109. echo 'error SQL: '.$query;
  110. die();
  111. }
  112. return $response;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement