]> git.wh0rd.org Git - tt-rss.git/blob - db.php
TAG 1.2.23-final
[tt-rss.git] / db.php
1 <?php
2
3 require_once "config.php";
4
5 function db_connect($host, $user, $pass, $db) {
6         if (DB_TYPE == "pgsql") {       
7                           
8                 $string = "dbname=$db user=$user password=$pass";       
9                 
10                 if ($host) {
11                         $string .= " host=$host";
12                 }
13
14                 if (defined('DB_PORT')) {
15                         $string = "$string port=" . DB_PORT;
16                 }
17
18                 $link = pg_connect($string);
19
20                 if (!$link) {
21                         die("Connection failed: " . pg_last_error($link));
22                 }
23
24                 return $link;
25
26         } else if (DB_TYPE == "mysql") {
27                 $link = mysql_connect($host, $user, $pass);
28                 if ($link) {
29                         $result = mysql_select_db($db, $link);                  
30                         if (!$result) {
31                                 die("Can't select DB: " . mysql_error($link));
32                         }                       
33                         return $link;
34                 } else {
35                         die("Connection failed: " . mysql_error($link));
36                 }
37         }
38 }
39
40 function db_escape_string($s) {
41         if (DB_TYPE == "pgsql") {       
42                 return pg_escape_string($s);
43         } else {
44                 return mysql_real_escape_string($s);
45         }
46 }
47
48 /* I hate MySQL :( */
49
50 function db_escape_string_2($s, $link) {
51         if (DB_TYPE == "pgsql") {       
52                 return pg_escape_string($s);
53         } else {
54                 return mysql_real_escape_string($s, $link);
55         }
56 }
57
58 function db_query($link, $query, $die_on_error = true) {
59         if (DB_TYPE == "pgsql") {
60                 $result = pg_query($link, $query);
61                 if (!$result) {
62                         $query = htmlspecialchars($query); // just in case
63                         if ($die_on_error) {
64                                 die("Query <i>$query</i> failed [$result]: " . pg_last_error($link));                   
65                         }
66                 }
67                 return $result;
68         } else if (DB_TYPE == "mysql") {
69                 $result = mysql_query($query, $link);
70                 if (!$result) {
71                         $query = htmlspecialchars($query);
72                         if ($die_on_error) {
73                                 die("Query <i>$query</i> failed: " . mysql_error($link));
74                         }
75                 }
76                 return $result;
77         }
78 }
79
80 function db_query_2($query) {
81         if (DB_TYPE == "pgsql") {
82                 return pg_query($query);
83         } else if (DB_TYPE == "mysql") {
84                 return mysql_query($link);
85         }
86 }
87
88 function db_fetch_assoc($result) {
89         if (DB_TYPE == "pgsql") {
90                 return pg_fetch_assoc($result);
91         } else if (DB_TYPE == "mysql") {
92                 return mysql_fetch_assoc($result);
93         }
94 }
95
96
97 function db_num_rows($result) {
98         if (DB_TYPE == "pgsql") {
99                 return pg_num_rows($result);
100         } else if (DB_TYPE == "mysql") {
101                 return mysql_num_rows($result);
102         }
103 }
104
105 function db_fetch_result($result, $row, $param) {
106         if (DB_TYPE == "pgsql") {
107                 return pg_fetch_result($result, $row, $param);
108         } else if (DB_TYPE == "mysql") {
109                 // I hate incoherent naming of PHP functions
110                 return mysql_result($result, $row, $param);
111         }
112 }
113
114 function db_unescape_string($str) {
115         $tmp = str_replace("\\\"", "\"", $str);
116         $tmp = str_replace("\\'", "'", $tmp);
117         return $tmp;
118 }
119
120 function db_close($link) {
121         if (DB_TYPE == "pgsql") {
122
123                 return pg_close($link);
124
125         } else if (DB_TYPE == "mysql") {
126                 return mysql_close($link);
127         }
128 }
129
130 function db_affected_rows($link, $result) {
131         if (DB_TYPE == "pgsql") {
132                 return pg_affected_rows($result);
133         } else if (DB_TYPE == "mysql") {
134                 return mysql_affected_rows($link);
135         }
136 }
137
138 function db_last_error($link) {
139         if (DB_TYPE == "pgsql") {
140                 return pg_last_error($link);
141         } else if (DB_TYPE == "mysql") {
142                 return mysql_error($link);
143         }
144 }
145
146 ?>