1
|
|
|
<?php |
2
|
|
|
class Db_Mysqli implements IDb { |
3
|
|
|
private $link; |
4
|
|
|
private $last_error; |
5
|
|
|
|
6
|
|
|
public function connect($host, $user, $pass, $db, $port) { |
7
|
|
|
if ($port) { |
8
|
|
|
$this->link = mysqli_connect($host, $user, $pass, $db, $port); |
9
|
|
|
} else { |
10
|
|
|
$this->link = mysqli_connect($host, $user, $pass, $db); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
if ($this->link) { |
14
|
|
|
$this->init(); |
15
|
|
|
|
16
|
|
|
return $this->link; |
17
|
|
|
} else { |
18
|
|
|
print("Unable to connect to database (as $user to $host, database $db): ".mysqli_connect_error()); |
19
|
|
|
exit(102); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function escape_string($s, $strip_tags = true) { |
24
|
|
|
if ($strip_tags) { |
25
|
|
|
$s = strip_tags($s); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return mysqli_real_escape_string($this->link, $s); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function query($query, $die_on_error = true) { |
32
|
|
|
$result = @mysqli_query($this->link, $query); |
33
|
|
|
if (!$result) { |
34
|
|
|
$this->last_error = @mysqli_error($this->link); |
35
|
|
|
|
36
|
|
|
@mysqli_query($this->link, "ROLLBACK"); |
|
|
|
|
37
|
|
|
user_error("query $query failed: ".($this->link ? $this->last_error : "No connection"), |
38
|
|
|
$die_on_error ? E_USER_ERROR : E_USER_WARNING); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $result; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function fetch_assoc($result) { |
45
|
|
|
return mysqli_fetch_assoc($result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
public function num_rows($result) { |
50
|
|
|
return mysqli_num_rows($result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function fetch_result($result, $row, $param) { |
54
|
|
|
if (mysqli_data_seek($result, $row)) { |
55
|
|
|
$line = mysqli_fetch_assoc($result); |
56
|
|
|
return $line[$param]; |
57
|
|
|
} else { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function close() { |
63
|
|
|
return mysqli_close($this->link); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function affected_rows($result) { |
67
|
|
|
return mysqli_affected_rows($this->link); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function last_error() { |
71
|
|
|
return mysqli_error($this->link); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function last_query_error() { |
75
|
|
|
return $this->last_error; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function init() { |
79
|
|
|
$this->query("SET time_zone = '+0:0'"); |
80
|
|
|
|
81
|
|
|
if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { |
|
|
|
|
82
|
|
|
mysqli_set_charset($this->link, MYSQL_CHARSET); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.