Location: PHPKode > scripts > DRBImageGallery - Free PHP Image Gallery > drbimagegallery/includes/db_utils.php
<?php
/****************************************************************************
 * DRBImageGallery
 * http://www.dbscripts.net/imagegallery/
 * 
 * Copyright (c) 2007-2009 Don Barnes 
 ****************************************************************************/
 
function db_error() {
	return mysql_error();
}

function db_connect() {
	global $DB_HOST;
	global $DB_USERNAME;
	global $DB_PASSWORD;
	global $DB_SCHEMA;

	$conn = mysql_connect($DB_HOST, $DB_USERNAME, $DB_PASSWORD);
	if($conn) {
		$db = mysql_select_db($DB_SCHEMA);
		if($db) {
			return $db;
		} else {
			die("Error selecting database: " . htmlspecialchars_default(db_error()));
		}
	} else {
		die("Error connecting to database: " . htmlspecialchars_default(db_error()));
	}
}

function db_insert_id() {
	return mysql_insert_id();
}

function db_execute($stmt) {
	//echo "<br />" . $stmt . "<br />\n"; // DEBUG
	$ret = mysql_query($stmt);
	if($ret === FALSE) {
		global $dbs_error;
		$dbs_error = mysql_error();
	}
	return $ret;
}

function db_execute_fetch($stmt) {
	$result = db_execute($stmt);
	if($result === FALSE) return FALSE; else return db_fetch($result);
}

function db_free_result($res) {
	mysql_free_result($res);
}

function db_fetch($res) {
	return mysql_fetch_assoc($res);
}

function db_get_literal($value) {
	if(empty($value)) {
		return "NULL";
	}
	
	$escapedValue = mysql_real_escape_string("" . $value);
	
	if(is_numeric($value)) {
		return $escapedValue;
	} else {
		return "'" . $escapedValue . "'";
	}
} 

function db_insert($tableBaseName, $values, $literalValues) {
	$tableName = full_table_name($tableBaseName);
	$sqlColumns = "";
	$sqlValues = "";

	foreach ($values as $columnName => $value) {
		if($sqlColumns != "") {
			$sqlColumns .= ", ";
			$sqlValues .= ", ";			
		}
		$sqlColumns .= mysql_real_escape_string($columnName);
		$sqlValues .= db_get_literal($value);
	}
	
	if(isset($literalValues)) {
		
		foreach ($literalValues as $columnName => $value) {
			if($sqlColumns != "") {
				$sqlColumns .= ", ";
				$sqlValues .= ", ";			
			}
			$sqlColumns .= mysql_real_escape_string($columnName);
			$sqlValues .= $value;
		}
		
	}
	
	$sql = "insert into {$tableName} (" . $sqlColumns . ") values (" . $sqlValues . ")";
	
	return db_execute($sql);
}

function db_update($tableBaseName, $values, $where) {
	$tableName = full_table_name($tableBaseName);
	
	$sqlColumns = "";
	foreach ($values as $columnName => $value) {
		if($sqlColumns != "") {
			$sqlColumns .= ", ";
		}
		$sqlColumns .= mysql_real_escape_string($columnName) . " = " . db_get_literal($value);
	}
	$sql = "update {$tableName} set " . $sqlColumns . " where " . $where;
	
	return db_execute($sql);
}

function db_count($basetablename, $where = NULL) {
	$tablename = full_table_name($basetablename);
	$stmt = "select count(*) as cnt from " . $tablename . " a";
	if(isset($where)) {
		$stmt .= " where " . $where;
	}
	$res = db_execute_fetch($stmt);
	return $res['cnt'];
}


function db_find($basetablename, $where = NULL, $orderby = NULL, $offset = 0, $count = NULL) {

	$tablename = full_table_name($basetablename);
	$stmt = "select * from " . $tablename . " a";
	if(isset($where)) {
		$stmt .= " where " . $where;
	}
	if(isset($orderby)) {
		$stmt .= " order by " . $orderby;
	}
	if(isset($offset) && isset($count)) {
		$stmt .= " limit " . $offset . "," . $count;
	}
	return db_execute($stmt);
}

function db_find_fetch($basetablename, $where) {
	$tablename = full_table_name($basetablename);
	return db_execute_fetch("select * from " . $tablename . " where " . $where);
}

function full_table_name($tablename) {
	global $TABLE_PREFIX;
	return $TABLE_PREFIX . $tablename;
}

function db_does_table_exist($tableBaseName) {
	$tablename = full_table_name($tableBaseName);
	$stmt = "show tables like '" . $tablename . "'";
	return db_execute_fetch($stmt) !== FALSE;
}

?>
Return current item: DRBImageGallery - Free PHP Image Gallery