]> git.wh0rd.org - tt-rss.git/blob - classes/db/mysql.php
reinstate error handlers; better DB error reporting on failed queries
[tt-rss.git] / classes / db / mysql.php
1 <?php
2 class Db_Mysql implements IDb {
3 private $link;
4
5 function connect($host, $user, $pass, $db, $port) {
6 $this->link = mysql_connect($host, $user, $pass);
7 if ($this->link) {
8 $result = mysql_select_db($db, $this->link);
9 if (!$result) {
10 die("Can't select DB: " . mysql_error($this->link));
11 }
12
13 $this->init();
14
15 return $this->link;
16 } else {
17 die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
18 }
19 }
20
21 function escape_string($s, $strip_tags = true) {
22 return mysql_real_escape_string($s, $this->link);
23 }
24
25 function query($query, $die_on_error = true) {
26 $result = mysql_query($query, $this->link);
27 if (!$result) {
28 user_error("Query $query failed: " . ($this->link ? mysql_error($this->link) : "No connection"),
29 $die_on_error ? E_USER_ERROR : E_USER_WARNING);
30 }
31 return $result;
32 }
33
34 function fetch_assoc($result) {
35 return mysql_fetch_assoc($result);
36 }
37
38
39 function num_rows($result) {
40 return mysql_num_rows($result);
41 }
42
43 function fetch_result($result, $row, $param) {
44 return mysql_result($result, $row, $param);
45 }
46
47 function close() {
48 return mysql_close($this->link);
49 }
50
51 function affected_rows($result) {
52 return mysql_affected_rows($this->link);
53 }
54
55 function last_error() {
56 return mysql_affected_rows($this->link);
57 }
58
59 function init() {
60 $this->query("SET time_zone = '+0:0'");
61
62 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
63 $this->query("SET NAMES " . MYSQL_CHARSET);
64 }
65
66 return true;
67 }
68
69 }
70 ?>