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