3 require_once "config.php";
5 function db_connect($host, $user, $pass, $db) {
6 if (DB_TYPE == "pgsql") {
8 $string = "dbname=$db user=$user password=$pass";
11 $string .= " host=$host";
14 if (defined('DB_PORT')) {
15 $string = "$string port=" . DB_PORT;
18 $link = pg_connect($string);
21 die("Connection failed: " . pg_last_error($link));
26 } else if (DB_TYPE == "mysql") {
27 $link = mysql_connect($host, $user, $pass);
29 $result = mysql_select_db($db, $link);
31 die("Can't select DB: " . mysql_error($link));
35 die("Connection failed: " . mysql_error($link));
40 function db_escape_string($s) {
41 if (DB_TYPE == "pgsql") {
42 return pg_escape_string($s);
44 return mysql_real_escape_string($s);
50 function db_escape_string_2($s, $link) {
51 if (DB_TYPE == "pgsql") {
52 return pg_escape_string($s);
54 return mysql_real_escape_string($s, $link);
58 function db_query($link, $query, $die_on_error = true) {
59 if (DB_TYPE == "pgsql") {
60 $result = pg_query($link, $query);
62 $query = htmlspecialchars($query); // just in case
64 die("Query <i>$query</i> failed [$result]: " . pg_last_error($link));
68 } else if (DB_TYPE == "mysql") {
69 $result = mysql_query($query, $link);
71 $query = htmlspecialchars($query);
73 die("Query <i>$query</i> failed: " . mysql_error($link));
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);
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);
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);
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);
114 function db_unescape_string($str) {
115 $tmp = str_replace("\\\"", "\"", $str);
116 $tmp = str_replace("\\'", "'", $tmp);
120 function db_close($link) {
121 if (DB_TYPE == "pgsql") {
123 return pg_close($link);
125 } else if (DB_TYPE == "mysql") {
126 return mysql_close($link);
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);
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);