-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
80 lines (56 loc) · 2.68 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace app;
use app\models\Product;
use PDO;
class Database{
public PDO $pdo;
//instantiate of database to access public
public static Database $db;
public function __construct(){
$this->pdo = new PDO('mysql:host=localhost;port=3306;dbname=betechatours_scandiweb_db', 'betechatours_gibson','@Nelsonel01');
// this->pdo = new PDO('mysql:host=localhost;port=3306;dbname= composer_crud_db_01', 'root','123#@!??SiteA');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$db = $this;
}
public function getProducts($search = ''){
if ($search) {
$statement = $this->pdo->prepare('SELECT * FROM products WHERE title like :search ORDER BY create_at DESC');
$statement->bindValue(":search", "%$search%");
} else {
$statement = $this->pdo->prepare('SELECT * FROM products ORDER BY create_at DESC');
}
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
///To get product id froom database for product to be updated
public function getProductById($id){
$statement = $this->pdo->prepare('SELECT * FROM products WHERE id = :id');
$statement->bindValue(':id', $id);
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
}
public function updateProduct(Product $product){
$statement = $this->pdo->prepare("UPDATE products SET title = :title,image = :image,description = :description,price = :price WHERE id = :id");
$statement->bindValue(':title', $product->title);
$statement->bindValue(':image', $product->imagePath);
$statement->bindValue(':description', $product->description);
$statement->bindValue(':price', $product->price);
$statement->bindValue(':id', $product->id);
$statement->execute();
}
public function deleteProduct($id){
$statement = $this->pdo->prepare('DELETE FROM products WHERE id = :id');
$statement->bindValue(':id', $id);
return $statement->execute();
}
public function createProduct(Product $product){
$statement = $this->pdo->prepare("INSERT INTO products (title, image, description, price, create_at)
VALUES (:title, :image, :description, :price, :date)");
$statement->bindValue(':title', $product->title);
$statement->bindValue(':image', $product->imagePath);
$statement->bindValue(':description', $product->description);
$statement->bindValue(':price', $product->price);
$statement->bindValue(':date', date('Y-m-d H:i:s'));
$statement->execute();
}
}