Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: iNs8T
- * Date: 03/02/16
- * Time: 13:42
- */
- class Model
- {
- private $id;
- private $pdocon;
- private $dsn, $username, $password;
- function __construct()
- {
- /*$this->dsn = 'mysql:host=127.0.0.1:3306;dbname=products';
- $this->username = 'root';
- $this->password = '';*/
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- }
- private function openConnection()
- {
- try {
- if ($this->pdocon == NULL) {
- $this->pdocon = new PDO($this->dsn, $this->username, $this->password);
- }
- }catch (PDOException $pdoexp) {
- $this->pdocon = NULL;
- throw new Exception('Database error');
- }
- }
- public function getAllItems() {
- try {
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- $pdocon = new PDO($this->dsn, $this->username, $this->password);
- $pdoStatement = $pdocon->prepare('SELECT * FROM products');
- $pdoStatement->execute();
- $products = $pdoStatement->fetchAll();
- $pdocon = NULL;
- return $products;
- }catch (PDOException $pdoexp) {
- $pdocon = NULL;
- throw $pdoexp;
- }
- }
- public function getAllMensTshirt() {
- try {
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- $pdocon = new PDO($this->dsn, $this->username, $this->password);
- $pdoStatement = $pdocon->prepare('SELECT * FROM products where gender = "M"');
- $pdoStatement->execute();
- $products = $pdoStatement->fetchAll();
- $pdocon = NULL;
- return $products;
- }catch (PDOException $pdoexp) {
- $pdocon = NULL;
- throw $pdoexp;
- }
- }
- public function getAllWomenTshirt() {
- try {
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- $pdocon = new PDO($this->dsn, $this->username, $this->password);
- $pdoStatement = $pdocon->prepare('SELECT * FROM products where gender = "W"');
- $pdoStatement->execute();
- $products = $pdoStatement->fetchAll();
- $pdocon = NULL;
- return $products;
- }catch (PDOException $pdoexp) {
- $pdocon = NULL;
- throw $pdoexp;
- }
- }
- public function getProductById($id){
- try{
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('SELECT * FROM products WHERE id = :mupp');
- $pdoStatement->bindParam(':mupp', $id);
- $pdoStatement->execute();
- $cartArray = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $cartArray;
- }catch(PDOException $pdoexp) {
- $pdocon = NULL;
- throw new Exception("Databasfel");
- }
- }
- public function showProductById($id){
- try{
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('SELECT * FROM products WHERE id = :mupp');
- $pdoStatement->bindParam(':mupp', $id);
- $pdoStatement->execute();
- $product = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $product;
- }catch(PDOException $pdoexp) {
- $pdocon = NULL;
- throw new Exception("Databasfel");
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment