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