]> git.wh0rd.org - tt-rss.git/blob - classes/db/pdo.php
aaac892c61cc3662b96b5274492e70fe5eac85a6
[tt-rss.git] / classes / db / pdo.php
1 <?php
2 class Db_PDO implements IDb {
3 private $pdo;
4
5 function connect($host, $user, $pass, $db, $port) {
6 $connstr = DB_TYPE . ":host=$host;dbname=$db;charset=utf8";
7
8 try {
9 $this->pdo = new PDO($connstr, $user, $pass);
10 } catch (PDOException $e) {
11 die($e->getMessage());
12 }
13
14 return $this->pdo;
15 }
16
17 function escape_string($s, $strip_tags = true) {
18 if ($strip_tags) $s = strip_tags($s);
19
20 $qs = $this->pdo->quote($s);
21
22 return mb_substr($qs, 1, mb_strlen($qs)-2);
23 }
24
25 function query($query, $die_on_error = true) {
26 try {
27 return $this->pdo->query($query);
28 } catch (PDOException $e) {
29 user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING);
30 }
31 }
32
33 function fetch_assoc($result) {
34 try {
35 if ($result) {
36 return $result->fetch();
37 } else {
38 return null;
39 }
40 } catch (PDOException $e) {
41 user_error($e->getMessage(), E_USER_WARNING);
42 }
43 }
44
45 function num_rows($result) {
46 try {
47 if ($result) {
48 return $result->rowCount();
49 } else {
50 return false;
51 }
52 } catch (PDOException $e) {
53 user_error($e->getMessage(), E_USER_WARNING);
54 }
55 }
56
57 function fetch_result($result, $row, $param) {
58 $line = $this->fetch_assoc($result);
59
60 if ($line)
61 return $line[$param];
62 else
63 return null;
64
65 }
66
67 function close() {
68 $this->pdo = null;
69 }
70
71 function affected_rows($result) {
72 try {
73 if ($result) {
74 return $result->rowCount();
75 } else {
76 return null;
77 }
78 } catch (PDOException $e) {
79 user_error($e->getMessage(), E_USER_WARNING);
80 }
81 }
82
83 function last_error() {
84 return join(" ", $pdo->errorInfo());
85 }
86 }
87 ?>