Commit 7c181aba authored by Dries's avatar Dries
Browse files

- Made some improvements/updates to the database abstraction layer.
parent 46ea3659
<?php
// $Id$
require_once 'DB.php';
$db_type = substr($db_url, 0, strpos($db_url, "://"));
/*
** Setup a database connection:
*/
$db_handle = DB::connect($db_url);
if (DB::isError($db_handle)) {
die ("Database problem: ". $db_handle->getMessage());
}
$db_handle->setFetchMode(DB_FETCHMODE_ASSOC);
function db_query($sql, $debug = 0) {
global $db_handle;
$result = $db_handle->query($sql);
if ($debug) {
print "<p>query: $sql<br />"; // error:". $result->getMessage() ."</p>";
}
if (DB::isError($result)) {
watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($sql));
}
return $result;
}
function db_fetch_object($result) {
if ($result) {
return $result->fetchRow(DB_FETCHMODE_OBJECT);
}
}
function db_fetch_array($result) {
if ($result) {
return $result->fetchRow(DB_FETCHMODE_ASSOC);
}
}
function db_num_rows($result) {
if ($result) {
return $result->numRows($result);
}
if (file_exists("includes/database.$db_type.inc")) {
include_once "includes/database.$db_type.inc";
}
function db_result($result, $field = 0) {
if ($result) {
$tmp = $result->fetchRow(DB_FETCHMODE_ORDERED);
return $tmp[$field];
}
else {
include_once "includes/database.pear.inc";
}
function db_error($result) {
global $db_handle;
db_connect($db_url);
if (DB::isError($db_handle)) {
return 1;
}
return 0;
}
?>
\ No newline at end of file
<?php
function db_connect($url) {
$url = parse_url($url);
mysql_pconnect($url["host"], $url["user"], $url["pass"]) or die(mysql_error());
mysql_select_db(substr($url["path"], 1)) or die ("unable to select database");
// NOTE: we are using a persistent connection!
}
function db_query($query, $debug = 0) {
$result = mysql_query($query);
if ($debug) {
print "<p>query: $query<br />error:". mysql_error() ."</p>";
}
if ($result) {
return $result;
}
else {
watchdog("error", "database: ". mysql_error() ."\nquery: ". htmlspecialchars($query));
}
}
function db_fetch_object($result) {
if ($result) {
return mysql_fetch_object($result);
}
}
function db_fetch_array($result) {
if ($result) {
return mysql_fetch_array($result, MYSQL_ASSOC);
}
}
function db_num_rows($result) {
if ($result) {
return mysql_num_rows($result);
}
}
function db_result($result, $row = 0) {
if ($result && mysql_num_rows($result) > $row) {
return mysql_result($result, $row);
}
}
function db_error() {
return mysql_errno();
}
?>
\ No newline at end of file
<?php
// $Id$
require_once 'DB.php';
function db_connect($url) {
global $db_handle;
$db_handle = DB::connect($url);
if (DB::isError($db_handle)) {
die ("Database problem: ". $db_handle->getMessage());
}
$db_handle->setFetchMode(DB_FETCHMODE_ASSOC);
}
function db_query($query, $debug = 0) {
global $db_handle;
$result = $db_handle->query($query);
if ($debug) {
print "<p>query: $query<br />"; // error:". $result->getMessage() ."</p>";
}
if (DB::isError($result)) {
watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query));
}
return $result;
}
function db_fetch_object($result) {
if ($result) {
return $result->fetchRow(DB_FETCHMODE_OBJECT);
}
}
function db_fetch_array($result) {
if ($result) {
return $result->fetchRow(DB_FETCHMODE_ASSOC);
}
}
function db_num_rows($result) {
if ($result) {
return $result->numRows($result);
}
}
function db_result($result, $row = 0) {
if ($result && $result->numRows($result) > $row) {
$tmp = $result->fetchRow(DB_FETCHMODE_ORDERED);
return $tmp[$row];
}
}
function db_error($result) {
global $db_handle;
return DB::isError($db_handle);
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment