-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
108 lines (101 loc) · 4.03 KB
/
Database.php
File metadata and controls
108 lines (101 loc) · 4.03 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
include 'config.php';
class Database {
private static $instance = null;
private static $connection = null;
// Might want to change 3 variables below according to your mysql settings
private function __construct()
{
// Initialize and check connection
if(Database::$instance == null){
Database::$connection = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (Database::$connection->connect_error) {
die ("Connection failed: " . Database::$connection->connect_error);
}
// Initialize database selection
mysqli_select_db(Database::$connection, DB_NAME);
}
else{
die ("Attempt to instantiate another singleton Database!");
}
}
// Singleton feature
public static function getInstance()
{
if (Database::$instance == null)
{
Database::$instance = new Database();
}
return Database::$instance;
}
// Create an SQL Statement for inserting FLIP_DISBUREMENT table
public function insertToDatabase($array)
{
Database::getInstance();
$id = $array['id'];
$amount = $array['amount'];
$status = $array['status'];
$timestamp = $array['timestamp'];
$bank_code = $array['bank_code'];
$account_number = $array['account_number'];
$beneficiary_name = $array['beneficiary_name'];
$remark = $array['remark'];
$receipt = $array['receipt'];
$time_served = $array['time_served'];
$fee = $array['fee'];
$sql_query = "INSERT INTO `FLIP_DISBURSEMENT` VALUES
($id, $amount, '$status', '$timestamp', '$bank_code', '$account_number', '$beneficiary_name', '$remark','$receipt', '$time_served', $fee);";
Database::executeThenCommit($sql_query);
}
// Create an SQL Statement for updating FLIP_DISBUREMENT table
public function updateToDatabase($array)
{
Database::getInstance();
$id = $array['id'];
$status = $array['status'];
$receipt = $array['receipt'];
$time_served = $array['time_served'];
$fee = $array['fee'];
$sql_query = "UPDATE `FLIP_DISBURSEMENT`
SET STATUS = '$status', RECEIPT ='$receipt' ,TIME_SERVED = '$time_served'
WHERE ID = $id;";
Database::executeThenCommit($sql_query);
}
// Prepare SQL statement and then execute
public function executeThenCommit($sql_query){
$stmt = Database::$connection->prepare($sql_query);
$stmt->execute();
}
// Table Creation
public function migrateDatabase()
{
Database::getInstance();
$sql_query = "CREATE TABLE IF NOT EXISTS `FLIP_DISBURSEMENT` (
`ID` DOUBLE(25,1),
`AMOUNT` INT(15) NOT NULL,
`STATUS` VARCHAR(10),
`TIMESTAMP` DATETIME,
`BANK_CODE` VARCHAR(10) NOT NULL,
`ACCOUNT_NUMBER` VARCHAR(20) NOT NULL,
`BENEFICIARY_NAME` VARCHAR(20),
`REMARK` VARCHAR(50) NOT NULL,
`RECEIPT` VARCHAR(255),
`TIME_SERVED` DATETIME,
`FEE` INT(15),
PRIMARY KEY (`ID`)
);";
$success = mysqli_query(Database::$connection,$sql_query) or die (mysqli_error(Database::$connection));
if ($success) {
echo "Migration is run successfully!";
} else {
echo "Migration is not run successfully...";
}
}
// Closing connection, not needed at the moment
public function closeConnection()
{
Database::getInstance();
mysqli_close(Database::$connection);
}
}
?>