]> git.wh0rd.org Git - tt-rss.git/blob - classes/db/mysql.php
mention that language passed is read only
[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                 if ($strip_tags) $s = strip_tags($s);
24
25                 return mysql_real_escape_string($s, $this->link);
26         }
27
28         function query($query, $die_on_error = true) {
29                 $result = @mysql_query($query, $this->link);
30                 if (!$result) {
31                         $error = @mysql_error($this->link);
32
33                         @mysql_query("ROLLBACK", $this->link);
34                         user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
35                                 $die_on_error ? E_USER_ERROR : E_USER_WARNING);
36                 }
37                 return $result;
38         }
39
40         function fetch_assoc($result) {
41                 return mysql_fetch_assoc($result);
42         }
43
44
45         function num_rows($result) {
46                 return mysql_num_rows($result);
47         }
48
49         function fetch_result($result, $row, $param) {
50                 return mysql_result($result, $row, $param);
51         }
52
53         function close() {
54                 return mysql_close($this->link);
55         }
56
57         function affected_rows($result) {
58                 return mysql_affected_rows($this->link);
59         }
60
61         function last_error() {
62                 return mysql_error();
63         }
64
65         function init() {
66                 $this->query("SET time_zone = '+0:0'");
67
68                 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
69                         $this->query("SET NAMES " . MYSQL_CHARSET);
70                 }
71
72                 return true;
73         }
74
75 }
76 ?>