Location: PHPKode > scripts > dbwrap > dbwrap/dbwrap.php
<?php

function  log_error($class, $function, $arguments, $message, $severity)	{
	// This is a very simple error logger, used by this class - OK for development, but not for production times -
	// right now I''m too busy developing a normal one, will try to update it later.
	// Just a few hints - if you''re to log errors - keep in mind that database may fail,
	// so you may want to redirect input to log file, or to email - and that is where severity levels will come useful -
	// if it''s something unimportant - use NOTICE, and log it to database; if database screwed up, however -
	// that''s a major failure, use ALERT and log to everything you can, just in case - database, files, email.
	// WARNING hangs in between those two, obviously.
	echo "<br><br><b>ERROR</b> <br> Class: ". $class. " <br> Function: ". $function ."<br> Message: ". $message;
	return true;
}

// constants for error logger, have to be accessible from everywhere
define ("ALERT", 0);
define ("WARNING", 1);
define ("NOTICE", 2);

class Database	{
	// Provides wrapper functions to database functions.
	/***************************************************************
	* Developed by Eugene Tsarovski
	* Distributed under GPL licence.
	* Should you have any questions or comments - email me at hide@address.com
	***************************************************************/
var $link;	// link to database
var $result;	// raw result set, should be used by member functions of this class only
var $num_rows;	// number of returned rows in a last SELECT statement
var $num_affected_rows; // number of rows affected by last DELETE, UPDATE, or INSERT statement


	function Database($server, $username, $password, $database)	{
		// Constructor, connects to database, and sets link name (if specified) for the link to database
		// Returns link number on success, or false on failure.

			// try to connect
		if (!$this->link = @mysql_connect($server, $username, $password)	)	{
				// if failed - log it
			log_error("Database", "Database", "$server, $username, $password", "Unable to connect to database, mysql said: ".mysql_error(), ALERT );
			return false;
		}

			// now select databse
		if (!@mysql_select_db($database, $this->link)	)	{
				// if can't - log
			log_error("Database", "Database", "$server, $username, $password", "Unable to select database $database, mysql said: ".mysql_error(), ALERT );
			return false;
		}
		return true;
	}


	function connect($server, $username, $password, $database)	{
		// Connects to database, and sets link name (if specified) for the link to database
		// Returns link number on success, or false on failure.
		// Same as constructor
		// May be used to change a database, although creating a new object would probably be a better idea

			// try to connect
		if (!$this->link = @mysql_connect($server, $username, $password)	)	{
				// if failed - log it
			log_error("Database", "connect", "$server, $username, $password", "Unable to connect to database, mysql said: ".mysql_error(), ALERT );
			return false;
		}

			// now select databse
		if (!@mysql_select_db($database, $this->link)	)	{
				// if can't - log
			log_error("Database", "Database", "$server, $username, $password", "Unable to select database $database, mysql said: ".mysql_error(), ALERT );
			return false;
		}
		return true;
	}


	function query($query)	{
		// Runs given query on a database
		// Determines type of query (select, insert, update, or delete)
		// If query type is SELECT, and $cache_name is different from 0 - raw result set will be saved in $result
		// Default link name = 0, first database object will connect to
		// Returns number of returned rows from SELECT queries, number of affected rows for INSERT, UPDATE, DELETE queries on success,
		// 	or false on failure

			// get a chunk of string containing first word to determine type of query - Select, DELETE, UPDATE, or insert,
			// and convert it to upper case
		$query_type = strtoupper(substr($query, 0, 8));


			// now determine type of query and run it
		if (substr_count($query_type, "SELECT")	== 1)	{
				// this is a select query, try to run it
			$this->num_rows = false;
			if (! $this->result = @mysql_query($query, $this->link)	)	{
					// if error is encountered
				log_error("Database", "query", "$query", "Unable to run query $query, mysql said ". mysql_error(), WARNING);
				return false;
			}
			return $this->num_rows = @mysql_num_rows($this->result);
		}	// end if SELECT
		//------------------------------------------------------------------------------------

		if (substr_count($query_type, "INSERT")	== 1)	{
			$this->num_affected_rows = false;
				// this is an INSERT query, try to run it
			if (! @mysql_query(addslashes($query), $this->link)	)	{
					// if error is encountered
				log_error("Database", "query", "$query", "Unable to run query $query, mysql said ". mysql_error(), WARNING);
				return false;
			}
			return $this->num_affected_rows = @mysql_affected_rows($this->link);
		}	// end if INSERT
		//------------------------------------------------------------------------------------


		if (substr_count($query_type, "UPDATE")	== 1)	{
			$this->num_affected_rows = false;
				// this is an UPDATE query, try to run it
			if (! @mysql_query($query, $this->link)	)	{
					// if error is encountered
				log_error("Database", "query", "$query", "Unable to run query $query, mysql said ". mysql_error(), WARNING);
				return false;
			}
			return $this->num_affected_rows = @mysql_affected_rows($this->link);
		}	// end if UPDATE
		//------------------------------------------------------------------------------------

		if (substr_count($query_type, "DELETE")	== 1)	{
			$this->num_affected_rows = false;
				// this is a DELETE query, try to run it
			if (! @mysql_query($query, $this->link)	)	{
					// if error is encountered
				log_error("Database", "query", "$query", "Unable to run query $query, mysql said ". mysql_error(), WARNING);
				return false;
			}
				// if this function returns 0 - doesn't mean no rows were deleted - see manual on mysql_affected_rows()
			return $this->num_affected_rows = @mysql_affected_rows($this->link);

		}	// end if DELETE
		//------------------------------------------------------------------------------------

			// in case wrong query type was submitted - not a SELECT, UPDATE, or DELETE - log error, return false
		log_error("Database", "query", "$query", "Wrong query type, only SELECT, UPDATE, and DELETE are supported", WARNING);
		return false;

	}	// end query()


	function fetch_result()	{
		// Fetches last result from SELECT query in 2-dimensional array
		// Returns array on success, false on failure
		if (!$this->num_rows)	{	// if result is empty - quit; means there's a bug in a code - does not check if query() returned results
			log_error("Database", "fetch_result", "$result", "Tried to fetch empty result set", WARNING);
			return false;
		}
		for ($i = 0; $i < $this->num_rows; $i++)	{
			$rows[$i] = @mysql_fetch_array($this->result, $this->link);
		}
		return $rows;
	}

	function num_rows()	{
		// Returns number of rows in a last result from SELECT query
		return $this->num_rows;
	}

	function affected_rows()		{
		// Returns number of affected row from last INSERT, UPDATE, or DELECT queries
		return $this->num_affected_rows;
	}
}	// end Database


?>
Return current item: dbwrap