-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqli.php
executable file
·82 lines (68 loc) · 1.65 KB
/
mysqli.php
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
<?php
class db {
private $link;
private $result;
private $sql;
public function __construct($hostname, $username, $password, $database) {
if (!$this->link = mysqli_connect($hostname, $username, $password, $database)) {
echo('Error: Could not make a database link using ');
}
$this->link->set_charset("utf8");
$this->link->query("SET SQL_MODE = ''");
}
public function query($sql) {
$query = $this->link->query($sql);
$this->sql = $sql;
if (!$this->link->errno){
if (isset($query->num_rows)) {
$data = array();
while ($row = $query->fetch_assoc()) {
$data[] = $row;
}
$result = new stdClass();
$result->num_rows = $query->num_rows;
$result->row = isset($data[0]) ? $data[0] : array();
$result->rows = $data;
unset($data);
$query->close();
$this->result = $result;
return $result;
} else{
return true;
}
} else {
throw new ErrorException('Error: ' . $this->link->error . '<br />Error No: ' . $this->link->errno . '<br />' . $sql);
exit();
}
}
public function fetch($index = ""){
$result = [];
foreach ($this->result->rows as $row) {
if($index=="")
$result[] = $row[key($row)];
elseif(isset($row[$index]))
$result[] = $row[$index];
}
return $result;
}
public function escape($value) {
if ($this->link) {
return mysqli_real_escape_string($this->link,$value);
}
}
public function count() {
if ($this->link) {
return mysqli_affected_rows($this->link);
}
}
public function lastId() {
if ($this->link) {
return mysqli_insert_id($this->link);
}
}
public function __destruct() {
if ($this->link) {
mysqli_close($this->link);
}
}
}