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