]> git.wh0rd.org Git - tt-rss.git/blob - classes/db/pdo.php
3020dea88a0c5d56063e832d0ae1c1d08252646f
[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 new Db_Stmt($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                 return $result->fetch_result($row, $param);
59         }
60
61         function close() {
62                 $this->pdo = null;
63         }
64
65         function affected_rows($result) {
66                 try {
67                         if ($result) {
68                                 return $result->rowCount();
69                         } else {
70                                 return null;
71                         }
72                 } catch (PDOException $e) {
73                         user_error($e->getMessage(), E_USER_WARNING);
74                 }
75         }
76
77         function last_error() {
78                 return join(" ", $pdo->errorInfo());
79         }
80 }
81 ?>