-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.database.php
More file actions
112 lines (87 loc) · 2.68 KB
/
Copy pathclass.database.php
File metadata and controls
112 lines (87 loc) · 2.68 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
<!-- Copyright © faysal fadel maulana 20190140097 -->
<?php
// database class
class database {
protected static $connection;
var $hostname="localhost";
var $username="root";
var $password="";
var $database="ptsawit";
public function __construct() {
}
public function connect() {
if(!isset(self::$connection)) {
self::$connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
}
// If connection was not successful, handle the error
if(self::$connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
self::$connection -> query("SET NAMES 'utf8'");
return self::$connection;
}
public function query($query) {
// Connect to the database
$connection = $this -> connect();
// Query the database
$result = $connection -> query($query);
return $result;
}
public function multi_query($query){
// Connect to the database
$connection = $this -> connect();
// Query the database
$result = $connection -> multi_query($query);
return $result;
}
public function insert($query) {
// Connect to the database
$connection = $this -> connect();
// Query the database
$connection -> query($query);
// Get inserted id
$insertid = $connection -> insert_id;
return $insertid;
}
public function select($query) {
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function num_rows($query) {
$result = $this -> query($query);
if($result === false) {
$count = 0;
}
else $count = $result->num_rows;
return $count;
}
/**
* Fetch the last error from the database
*
* @return string Database error message
*/
public function error() {
return self::$connection -> error;
}
/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
public function escape($value) {
$connection = $this -> connect();
return $connection -> real_escape_string(trim($value));
}
}
$database = new database();
?>
<!-- Copyright © faysal fadel maulana 20190140097 -->