Guest User

Untitled

a guest
Feb 21st, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Created by PhpStorm.
  5. * User: iNs8T
  6. * Date: 03/02/16
  7. * Time: 13:42
  8. */
  9.  
  10. class Model
  11. {
  12. private $id;
  13.  
  14. private $pdocon;
  15. private $dsn, $username, $password;
  16.  
  17. function __construct()
  18. {
  19. /*$this->dsn = 'mysql:host=127.0.0.1:3306;dbname=products';
  20. $this->username = 'root';
  21. $this->password = '';*/
  22.  
  23. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  24. $this->username = 'kayjays_se';
  25. $this->password = 'hej123';
  26. }
  27.  
  28. private function openConnection()
  29. {
  30. try {
  31. if ($this->pdocon == NULL) {
  32. $this->pdocon = new PDO($this->dsn, $this->username, $this->password);
  33. }
  34. }catch (PDOException $pdoexp) {
  35. $this->pdocon = NULL;
  36. throw new Exception('Database error');
  37. }
  38. }
  39.  
  40. public function getAllItems() {
  41. try {
  42. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  43. $this->username = 'kayjays_se';
  44. $this->password = 'hej123';
  45.  
  46. $pdocon = new PDO($this->dsn, $this->username, $this->password);
  47.  
  48. $pdoStatement = $pdocon->prepare('SELECT * FROM products');
  49. $pdoStatement->execute();
  50.  
  51. $products = $pdoStatement->fetchAll();
  52. $pdocon = NULL;
  53.  
  54. return $products;
  55. }catch (PDOException $pdoexp) {
  56. $pdocon = NULL;
  57. throw $pdoexp;
  58. }
  59. }
  60.  
  61. public function getAllMensTshirt() {
  62. try {
  63. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  64. $this->username = 'kayjays_se';
  65. $this->password = 'hej123';
  66.  
  67. $pdocon = new PDO($this->dsn, $this->username, $this->password);
  68.  
  69. $pdoStatement = $pdocon->prepare('SELECT * FROM products where gender = "M"');
  70. $pdoStatement->execute();
  71.  
  72. $products = $pdoStatement->fetchAll();
  73. $pdocon = NULL;
  74.  
  75. return $products;
  76. }catch (PDOException $pdoexp) {
  77. $pdocon = NULL;
  78. throw $pdoexp;
  79. }
  80. }
  81.  
  82. public function getAllWomenTshirt() {
  83. try {
  84. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  85. $this->username = 'kayjays_se';
  86. $this->password = 'hej123';
  87.  
  88. $pdocon = new PDO($this->dsn, $this->username, $this->password);
  89.  
  90. $pdoStatement = $pdocon->prepare('SELECT * FROM products where gender = "W"');
  91. $pdoStatement->execute();
  92.  
  93. $products = $pdoStatement->fetchAll();
  94. $pdocon = NULL;
  95.  
  96. return $products;
  97. }catch (PDOException $pdoexp) {
  98. $pdocon = NULL;
  99. throw $pdoexp;
  100. }
  101. }
  102.  
  103.  
  104. public function getProductById($id){
  105. try{
  106. $this->openConnection();
  107. $pdoStatement = $this->pdocon->prepare('SELECT * FROM products WHERE id = :mupp');
  108. $pdoStatement->bindParam(':mupp', $id);
  109.  
  110. $pdoStatement->execute();
  111.  
  112. $cartArray = $pdoStatement->fetchAll();
  113.  
  114. $this->pdocon = NULL;
  115.  
  116. return $cartArray;
  117.  
  118. }catch(PDOException $pdoexp) {
  119. $pdocon = NULL;
  120. throw new Exception("Databasfel");
  121. }
  122. }
  123.  
  124. public function showProductById($id){
  125. try{
  126. $this->openConnection();
  127. $pdoStatement = $this->pdocon->prepare('SELECT * FROM products WHERE id = :mupp');
  128. $pdoStatement->bindParam(':mupp', $id);
  129.  
  130. $pdoStatement->execute();
  131.  
  132. $product = $pdoStatement->fetchAll();
  133.  
  134. $this->pdocon = NULL;
  135.  
  136. return $product;
  137.  
  138. }catch(PDOException $pdoexp) {
  139. $pdocon = NULL;
  140. throw new Exception("Databasfel");
  141. }
  142. }
  143.  
  144.  
  145. }
  146. ?>
Add Comment
Please, Sign In to add comment