2005-09-07 12:17:16 +00:00
|
|
|
<?
|
|
|
|
|
|
|
|
require_once "config.php";
|
|
|
|
|
|
|
|
function db_connect($host, $user, $pass, $db) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
|
2006-03-21 10:38:41 +00:00
|
|
|
$string = "dbname=$db user=$user password=$pass";
|
2005-09-14 02:41:44 +00:00
|
|
|
|
|
|
|
if ($host) {
|
2005-09-14 13:37:53 +00:00
|
|
|
$string .= " host=$host";
|
2005-09-14 02:41:44 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 10:38:41 +00:00
|
|
|
if (defined('DB_PORT')) {
|
|
|
|
$string = "$string port=" . DB_PORT;
|
|
|
|
}
|
|
|
|
|
2005-09-21 05:47:17 +00:00
|
|
|
$link = pg_connect($string);
|
|
|
|
|
|
|
|
if (!$link) {
|
|
|
|
die("Connection failed: " . pg_last_error($link));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
2005-09-07 12:17:16 +00:00
|
|
|
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 05:47:17 +00:00
|
|
|
$link = mysql_connect($host, $user, $pass);
|
2005-09-07 12:17:16 +00:00
|
|
|
if ($link) {
|
2005-09-21 05:47:17 +00:00
|
|
|
$result = mysql_select_db($db, $link);
|
|
|
|
if (!$result) {
|
|
|
|
die("Can't select DB: " . mysql_error($link));
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
} else {
|
|
|
|
die("Connection failed: " . mysql_error($link));
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function db_escape_string($s) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_escape_string($s);
|
|
|
|
} else {
|
2005-09-07 13:31:21 +00:00
|
|
|
return mysql_real_escape_string($s);
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-13 01:05:47 +00:00
|
|
|
/* I hate MySQL :( */
|
|
|
|
|
|
|
|
function db_escape_string_2($s, $link) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_escape_string($s);
|
|
|
|
} else {
|
|
|
|
return mysql_real_escape_string($s, $link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-13 04:46:40 +00:00
|
|
|
function db_query($link, $query, $die_on_error = true) {
|
2005-09-07 12:17:16 +00:00
|
|
|
if (DB_TYPE == "pgsql") {
|
2005-09-21 05:47:17 +00:00
|
|
|
$result = pg_query($link, $query);
|
|
|
|
if (!$result) {
|
2005-10-16 08:52:44 +00:00
|
|
|
$query = htmlspecialchars($query); // just in case
|
2006-08-13 04:46:40 +00:00
|
|
|
if ($die_on_error) {
|
|
|
|
die("Query <i>$query</i> failed: " . pg_last_error($link));
|
|
|
|
}
|
2005-09-21 05:47:17 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2005-09-07 12:17:16 +00:00
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 05:47:17 +00:00
|
|
|
$result = mysql_query($query, $link);
|
|
|
|
if (!$result) {
|
2005-10-16 08:52:44 +00:00
|
|
|
$query = htmlspecialchars($query);
|
2006-08-13 04:46:40 +00:00
|
|
|
if ($die_on_error) {
|
|
|
|
die("Query <i>$query</i> failed: " . mysql_error($link));
|
|
|
|
}
|
2005-09-21 05:47:17 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-07 13:31:21 +00:00
|
|
|
function db_query_2($query) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_query($query);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 05:47:17 +00:00
|
|
|
return mysql_query($link);
|
2005-09-07 13:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-07 12:17:16 +00:00
|
|
|
function db_fetch_assoc($result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_fetch_assoc($result);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-21 05:47:17 +00:00
|
|
|
return mysql_fetch_assoc($result);
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function db_num_rows($result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
2005-09-07 13:31:21 +00:00
|
|
|
return pg_num_rows($result);
|
2005-09-07 12:17:16 +00:00
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-09-07 13:31:21 +00:00
|
|
|
return mysql_num_rows($result);
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function db_fetch_result($result, $row, $param) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_fetch_result($result, $row, $param);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
2005-10-16 08:19:40 +00:00
|
|
|
// I hate incoherent naming of PHP functions
|
|
|
|
return mysql_result($result, $row, $param);
|
2005-09-07 12:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-16 14:48:33 +00:00
|
|
|
function db_unescape_string($str) {
|
|
|
|
$tmp = str_replace("\\\"", "\"", $str);
|
|
|
|
$tmp = str_replace("\\'", "'", $tmp);
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2005-09-07 12:17:16 +00:00
|
|
|
function db_close($link) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
|
|
|
|
return pg_close($link);
|
|
|
|
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_close($link);
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 03:45:44 +00:00
|
|
|
|
2006-05-16 12:37:35 +00:00
|
|
|
function db_affected_rows($link, $result) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_affected_rows($result);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_affected_rows($link);
|
|
|
|
}
|
2006-05-16 12:56:53 +00:00
|
|
|
}
|
2006-08-16 07:28:10 +00:00
|
|
|
|
|
|
|
function db_last_error($link) {
|
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
return pg_last_error($link);
|
|
|
|
} else if (DB_TYPE == "mysql") {
|
|
|
|
return mysql_error($link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-17 03:45:44 +00:00
|
|
|
?>
|