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