]> git.wh0rd.org - tt-rss.git/blame - classes/db/mysql.php
more work on singleton-based DB
[tt-rss.git] / classes / db / mysql.php
CommitLineData
95947917
AD
1<?php
2class 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 }
404e2e36
AD
12
13 $this->init();
14
95947917
AD
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 $query = htmlspecialchars($query);
29 if ($die_on_error) {
30 die("Query <i>$query</i> failed: " . ($this->link ? mysql_error($link) : "No connection"));
31 }
32 }
33 return $result;
34 }
35
36 function fetch_assoc($result) {
37 return mysql_fetch_assoc($result);
38 }
39
40
41 function num_rows($result) {
42 return mysql_num_rows($result);
43 }
44
45 function fetch_result($result, $row, $param) {
46 return mysql_result($result, $row, $param);
47 }
48
49 function close() {
50 return mysql_close($this->link);
51 }
52
53 function affected_rows($result) {
54 return mysql_affected_rows($this->link);
55 }
56
57 function last_error() {
58 return mysql_affected_rows($this->link);
59 }
60
ba68b681
AD
61 function init() {
62 $this->query("SET time_zone = '+0:0'");
63
64 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
65 $this->query("SET NAMES " . MYSQL_CHARSET);
66 }
67
68 return true;
69 }
70
95947917
AD
71}
72?>