]> git.wh0rd.org - tt-rss.git/blame - db.php
update fr_FR translation
[tt-rss.git] / db.php
CommitLineData
1d3a17c7 1<?php
648472a7
AD
2
3require_once "config.php";
4
5function db_connect($host, $user, $pass, $db) {
6 if (DB_TYPE == "pgsql") {
7
befc807f 8 $string = "dbname=$db user=$user password=$pass";
6b3bce8a
AD
9
10 if ($host) {
387e6b41 11 $string .= " host=$host";
6b3bce8a
AD
12 }
13
befc807f
AD
14 if (defined('DB_PORT')) {
15 $string = "$string port=" . DB_PORT;
16 }
17
922b009f
AD
18 $link = pg_connect($string);
19
20 if (!$link) {
21 die("Connection failed: " . pg_last_error($link));
22 }
23
24 return $link;
648472a7
AD
25
26 } else if (DB_TYPE == "mysql") {
922b009f 27 $link = mysql_connect($host, $user, $pass);
648472a7 28 if ($link) {
922b009f
AD
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));
648472a7 36 }
648472a7
AD
37 }
38}
39
40function db_escape_string($s) {
41 if (DB_TYPE == "pgsql") {
42 return pg_escape_string($s);
43 } else {
8158c57a 44 return mysql_real_escape_string($s);
648472a7
AD
45 }
46}
47
aec3ce39 48function db_query($link, $query, $die_on_error = true) {
648472a7 49 if (DB_TYPE == "pgsql") {
922b009f
AD
50 $result = pg_query($link, $query);
51 if (!$result) {
8823cd59 52 $query = htmlspecialchars($query); // just in case
aec3ce39 53 if ($die_on_error) {
472782e8 54 die("Query <i>$query</i> failed [$result]: " . pg_last_error($link));
aec3ce39 55 }
922b009f
AD
56 }
57 return $result;
648472a7 58 } else if (DB_TYPE == "mysql") {
922b009f
AD
59 $result = mysql_query($query, $link);
60 if (!$result) {
8823cd59 61 $query = htmlspecialchars($query);
aec3ce39
AD
62 if ($die_on_error) {
63 die("Query <i>$query</i> failed: " . mysql_error($link));
64 }
922b009f
AD
65 }
66 return $result;
648472a7
AD
67 }
68}
69
70function db_fetch_assoc($result) {
71 if (DB_TYPE == "pgsql") {
72 return pg_fetch_assoc($result);
73 } else if (DB_TYPE == "mysql") {
922b009f 74 return mysql_fetch_assoc($result);
648472a7
AD
75 }
76}
77
78
79function db_num_rows($result) {
80 if (DB_TYPE == "pgsql") {
8158c57a 81 return pg_num_rows($result);
648472a7 82 } else if (DB_TYPE == "mysql") {
8158c57a 83 return mysql_num_rows($result);
648472a7
AD
84 }
85}
86
87function db_fetch_result($result, $row, $param) {
88 if (DB_TYPE == "pgsql") {
89 return pg_fetch_result($result, $row, $param);
90 } else if (DB_TYPE == "mysql") {
e212e483
AD
91 // I hate incoherent naming of PHP functions
92 return mysql_result($result, $row, $param);
648472a7
AD
93 }
94}
95
6e0584e9
AD
96function db_unescape_string($str) {
97 $tmp = str_replace("\\\"", "\"", $str);
98 $tmp = str_replace("\\'", "'", $tmp);
99 return $tmp;
100}
101
648472a7
AD
102function db_close($link) {
103 if (DB_TYPE == "pgsql") {
104
105 return pg_close($link);
106
107 } else if (DB_TYPE == "mysql") {
108 return mysql_close($link);
109 }
110}
7b099a4c 111
fea67e6a
AD
112function db_affected_rows($link, $result) {
113 if (DB_TYPE == "pgsql") {
114 return pg_affected_rows($result);
115 } else if (DB_TYPE == "mysql") {
116 return mysql_affected_rows($link);
117 }
68d3cf5a 118}
a4dbc524
AD
119
120function db_last_error($link) {
121 if (DB_TYPE == "pgsql") {
122 return pg_last_error($link);
123 } else if (DB_TYPE == "mysql") {
124 return mysql_error($link);
125 }
126}
127
7b099a4c 128?>