-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.php
More file actions
120 lines (104 loc) · 3.21 KB
/
Copy pathDatabase.php
File metadata and controls
120 lines (104 loc) · 3.21 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
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Simple PHP MySQLi Database class for PHP7.* & PHP8.*
* ----------------------------------------
* This class provides a simple way to connect to your database and execute your queries.
* It uses prepared statements to prevent SQL injections.
*
* @author LH
* @link https://webdeasy.de/en/php-mysql-database-class
*/
class Database {
private $host, $database, $username, $password, $connection; // connection credentials
private $port = 3306; // default port
private $affected_rows; // properties to save before executing next query
/**
* Sets the connection credentials to connect to your database.
*
* @param string $host - the host of your database
* @param string $username - the username of your database
* @param string $password - the password of your database
* @param string $database - your database name
* @param integer $port - the port of your database
* @param boolean $autoconnect - to auto connect to the database after settings connection credentials
*/
function __construct($host, $username, $password, $database, $port = 3306, $autoconnect = true) {
$this->host = $host;
$this->database = $database;
$this->username = $username;
$this->password = $password;
$this->port = $port;
if ($autoconnect) $this->open();
}
/**
* Open the connection to your database.
*/
function open() {
$this->connection = new mysqli($this->host, $this->username, $this->password, $this->database, $this->port);
}
/**
* Close the connection to your database.
*/
function close() {
$this->connection->close();
}
/**
*
* Execute your query
*
* @param string $query - your sql query
* @param array $parameters - your parameters to bind to your query
* @return mysqli_result result of the executed query
*/
function query($query, $parameters = array()) {
// reset data of last query
$this->affected_rows = 0;
// prepare the query
$stmt = $this->connection->prepare($query);
// check if prepare statement failed
if ($stmt === false) {
die("Error in prepare statement: " . $this->connection->error);
}
// check if parameters are given
if (!empty($parameters)) {
$types = "";
$bindParams = [];
// get the types of the parameters
foreach ($parameters as $param) {
if (is_int($param)) {
$types .= "i";
} elseif (is_double($param)) {
$types .= "d";
} else {
$types .= "s";
}
$bindParams[] = $param;
}
// bind the parameters to the query
$stmt->bind_param($types, ...$bindParams);
}
// execute the query
if ($stmt->execute()) {
$result = $stmt->get_result();
$this->affected_rows = $stmt->affected_rows;
$stmt->close();
return $result;
} else {
die("Error executing query: " . $stmt->error);
}
}
/**
* Get the amount of affected rows of the last executed query.
* @return integer
*/
function get_affected_rows() {
return $this->affected_rows;
}
/**
* Get the last inserted id of the last executed query.
* @return integer
*/
function get_last_inserted_id() {
return $this->connection->insert_id;
}
}