Advertisement
Guest User

Untitled

a guest
Mar 4th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 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 = 'root'; //username
  15. $this->password = ''; //password
  16. $this->baseName = 'blog'; //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 execute($query) {
  85. if (!$response = $this->conn->exec($query)) {
  86. echo 'PDO::errorInfo():';
  87. echo '<br />';
  88. echo 'error SQL: '.$query;
  89. die();
  90. }
  91. return $response;
  92. }
  93. }
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement