]> git.wh0rd.org Git - tt-rss.git/blob - classes/db/mysql.php
Merge pull request #186 from EyesX/master
[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                         user_error("Query $query failed: " . ($this->link ? mysql_error($this->link) : "No connection"),
32                                 $die_on_error ? E_USER_ERROR : E_USER_WARNING);
33                 }
34                 return $result;
35         }
36
37         function fetch_assoc($result) {
38                 return mysql_fetch_assoc($result);
39         }
40
41
42         function num_rows($result) {
43                 return mysql_num_rows($result);
44         }
45
46         function fetch_result($result, $row, $param) {
47                 return mysql_result($result, $row, $param);
48         }
49
50         function close() {
51                 return mysql_close($this->link);
52         }
53
54         function affected_rows($result) {
55                 return mysql_affected_rows($this->link);
56         }
57
58         function last_error() {
59                 return mysql_error();
60         }
61
62         function init() {
63                 $this->query("SET time_zone = '+0:0'");
64
65                 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
66                         $this->query("SET NAMES " . MYSQL_CHARSET);
67                 }
68
69                 return true;
70         }
71
72 }
73 ?>