Repository navigation
Expand file tree
/
Copy pathDbConnect_MySQLI.php
More file actions
66 lines (47 loc) · 1.65 KB
/
Copy pathDbConnect_MySQLI.php
File metadata and controls
66 lines (47 loc) · 1.65 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
<?php
/**
* Database Connection
*/
class DbConnect_MYSQLI {
private $server = 'localhost';
private $dbname = 'database';
private $user = 'user';
private $pass = 'password';
private $mysqli;
private $sql;
private $result;
private $fetchMode = MYSQLI_ASSOC;
function __construct() {
$this->mysqli = mysqli_connect($this->server, $this->user, $this->pass, $this->dbname);
if (mysqli_connect_errno()) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$this->mysqli->query("SET NAMES 'utf8'");
$this->mysqli->query("SET CHARACTER SET utf8");
}
function query($sql) {
$this->sql = $this->mysqli->real_escape_string($sql);
$this->result = $this->mysqli->query($sql);
if ($this->result == true) {
$data = [];
while ($row = $this->result->fetch_array($this->fetchMode)) {
$data[] = $row;
}
$this->result->close();
return $data;
}
else {
printf("<b>Problem with SQL:</b> %s\n", $this->sql);
exit;
}
}
public function id() {
return $this->mysqli->insert_id;
}
public function __destruct() {
$this->mysqli->close();
}
}