-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.php
More file actions
36 lines (32 loc) · 1.11 KB
/
Copy pathsql.php
File metadata and controls
36 lines (32 loc) · 1.11 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
<?php
$ini_array = parse_ini_file("dbcreds.ini");
$connection = new SQLConnection($ini_array['ip'], $ini_array['username'], $ini_array['password'], $ini_array['schema']);
unset($ini_array);
class SQLConnection {
private $conn;
function __construct(string $server, string $uname, string $passwd, string $schema) {
$this->conn = new mysqli($server, $uname, $passwd, $schema);
if ($this->conn->connect_errno) {
exit("SQL connection failed: $this->conn->connect_error");
}
}
/**
* Runs a SQL query
* @return array|bool if the query returns results, an array is returned, otherwise, a boolean representing
* the success of the operation is returned
*/
function query(string $sql): array | bool {
$result = $this->conn->query($sql);
if ($result instanceof mysqli_result) {
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
return $result;
}
function escape_string(string $str) {
return mysqli_escape_string($this->conn, $str);
}
function __destruct() {
$this->conn->close();
}
}
?>