- Made some improvements/updates to the database abstraction layer.

4.0.x
Dries Buytaert 2001-10-31 20:33:23 +00:00
parent 46ea3659dd
commit 7c181aba6d
3 changed files with 127 additions and 59 deletions

View File

@ -1,67 +1,14 @@
<?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());
if (file_exists("includes/database.$db_type.inc")) {
include_once "includes/database.$db_type.inc";
}
$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;
else {
include_once "includes/database.pear.inc";
}
function db_fetch_object($result) {
if ($result) {
return $result->fetchRow(DB_FETCHMODE_OBJECT);
}
}
db_connect($db_url);
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, $field = 0) {
if ($result) {
$tmp = $result->fetchRow(DB_FETCHMODE_ORDERED);
return $tmp[$field];
}
}
function db_error($result) {
global $db_handle;
if (DB::isError($db_handle)) {
return 1;
}
return 0;
}
?>

View File

@ -0,0 +1,56 @@
<?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();
}
?>

View File

@ -0,0 +1,65 @@
<?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);
}
?>