Location: PHPKode > projects > OvBB > ovbb/upload/includes/mysql.inc.php
<?php
//***************************************************************************//
//                                                                           //
//  Copyright (c) 2007 Jonathon Freeman                                      //
//  Copyright (c) 2007 Brian Otto                                            //
//  All rights reserved.                                                     //
//                                                                           //
//  This program is free software. You may use, modify, and/or redistribute  //
//  it under the terms of the MIT License.                                   //
//                                                                           //
//***************************************************************************//

// Special query constants
define('CONCAT_ATTACHMENT',  0);
define('CONCAT_AVATAR',      1);

// Class definition
class DBConnection
{
	var $objConnection;
	var $objSelect;
	var $objResult;
	var $aVersions = array('0.16a');
	var $aSpecialQueries = array();

	// Initializes the database connection.
	function DBConnection($aDBInfo)
	{
		global $aQueries;

		// Initialize query array.
		$aQueries = array();

		// Populate the special queries list.
		$this->aSpecialQueries[CONCAT_ATTACHMENT] = "UPDATE attachment SET filedata = concat(filedata, '%s') WHERE id=%u";
		$this->aSpecialQueries[CONCAT_AVATAR] = "UPDATE avatar SET datum = concat(datum, '%s') WHERE id=%u";

		// Try and connect to the database server.
		if($this->objConnection = @mysql_connect($aDBInfo['address'], $aDBInfo['username'], $aDBInfo['password']))
		{
			// Try and select our database.
			if(@mysql_select_db($aDBInfo['database']))
			{
				// Destroy the database connection information.
				unset($aDBInfo['address']);
				unset($aDBInfo['username']);
				unset($aDBInfo['password']);
				unset($aDBInfo['database']);

				// Flag selection success.
				$this->objSelect = TRUE;
			}
			else
			{
				// Flag selection failure.
				$this->objSelect = FALSE;
			}
		}
		else
		{
			// Flag connection failure.
			$this->objConnection = FALSE;
		}
	}

	// Executes a given SQL statement.
	function query($strSQL)
	{
		global $aQueries;

		// Save the query.
		$aQueries[] = $strSQL;

		// Execute the query.
		$this->objResult = @mysql_query($strSQL);

		// Return the result.
		if($this->objResult === FALSE)
		{
			DatabaseError();
		}
		else
		{
			return TRUE;
		}
	}

	// Executes a special, pre-defined query.
	function squery($iQueryID)
	{
		// Get the function arguments.
		$aArgs = func_get_args();

		// The first argument is supposedly the index into the special queries array,
		// so use it to grab the special query requested.
		$aArgs[0] = $this->aSpecialQueries[$iQueryID];

		// Create the SQL statement.
		$strSQL = call_user_func_array('sprintf', $aArgs);

		// Execute the query.
		return $this->query($strSQL);
	}

	// Retrieves a resultset as an array.
	function getresult($bIsAssoc = FALSE)
	{
		return($bIsAssoc ? mysql_fetch_assoc($this->objResult) : mysql_fetch_row($this->objResult));
	}

	// Retrieves all the resultsets.
	function getall($bIsAssoc = FALSE)
	{
		$allResults = array();

		while($result = $this->getresult($bIsAssoc))
		{
			$allResults[] = $result;
		}

		return $allResults;
	}

	// Returns the ID generated by the last INSERT.
	function getinsertid()
	{
		return mysql_insert_id($this->objConnection);
	}

	function geterror()
	{
		return mysql_error();
	}

	// Sanitizes the input for use in a query.
	function sanitize($str)
	{
		return mysql_real_escape_string($str, $this->objConnection);
	}

	// Unescapes binary data return in a result.
	function unescape($str)
	{
		return $str;
	}
}
?>
Return current item: OvBB