All pastes #963729 Raw Edit

Unnamed

public php v1 · immutable
#963729 ·published 2008-03-30 19:40 UTC
rendered paste body
<?php/** * Database class * @par handles all database interactoins. * @since 0.0 */class Database {	private $database; // database name that we connect to	private $host; // location of the host. Usually localhost	private $user; // username of the db user	private $password; // pass of the db user	private $port; // port to access in the database.	private $connection_id; // database connection handle.	private $debug; // are we debugging? (0 if not)	private $query_id; // query handle	private $query; // current query.	/**	 * Constructor	 * @param database String that contains the database name	 * that we wish to connect to.	 * @param host Host to connect to, this usually is 'localhost'	 * @param user database user to connect with. This is not	 * the actual user of the application!	 * @param password Database password for the database user.	 * @param port Defaults to nothing, unless we are connecting	 * to a database not on localhost, a port is not required.	 * @param debug Set to 1 if we need to debug the actual	 * mysql queries.	 */	public function __construct ($database,		$host, $user, $pasword, $port = '', $debug = 0) {		$this->database = $database;		$this->host = $host;		$this->user = $user;		$this->password = $password;		$this->debug = $debug;		//should we do connectoin now or call independently?	}	public function __destruct() {	}	/**	 * @return bool - true if ok, false if failure.	 */	public function connect() {		//Check if we are already connected. If we are		//connectoin_id will be some value other then empty.		if(empty($this->connection_id)) {			if(!empty($this->port)) {				mysqli_connect($this->host . ':' . $this->port, $this->user, $this->password);			} else {				mysqli_connect($this->host, $this->user, $this->password);			}			//recheck that we actually *are* connected now.			if(!$this->connection_id) {				$this->debug_out('unable to connect to host ' . $this->host . ' port ' . $this->port . ' database ' . $this->database . ' user ' . $this->user . '.');				return false;			}		} else { //we are already connected.			$this->debug_out('We already are connected to host. Check that we are not attempting to connect twice in a row.');			return false;		}		return true;//all went well	}	/** Disconnect from database	 * @return true if ok, otherwise false on any type of failure The	 * primary way that this will fail is if there is no database	 * already connected.	 */	public function disconnect() {		//an empty connection_id indicates that there is no connection.		if (!empty($this->connection_id)) {			return mysqli_close($this->connection_id);		} else {			$this->stop('Cannot disconnect, no database connected');			return false;		}	}	/**	 * @param query query sanitized before hand.	 * @return TRUE or FALSE, if false, query failed.	 * @par use this function for querys that do READ only access.	 * the reason for this is so that we can easily do cache's later	 * if desired.	 * @warning Please make sure that all user input is sanitized with the	 * sanitize function before attempting to query the table. Don't	 * let bobby tables happen to us! (bobby tables refers to MYSQL injection)	 */	public function select($query) {		return query($query);	}	/**	 * Query the database to write to it.	 * @param query Mysql query, sanitized before hand.	 * @return TRUE or FALSE, if false, query failed	 * @par This is intended for doing all writes. You *can* do reads,	 * but this is not intended. Please use select above for all select	 * queries that do not actually *modify* the database. This is so	 * in the future we can easily make use of a cache.	 * @warning Please make sure that all user input is sanitized with the	 * sanitize function before attempting to query the table. Don't	 * let bobby tables happen to us! (bobby tables refers to MYSQL injection)	 */	public function query($query) {		$this->query = $query;		//run query, check if problems exist. If we have problems		//do different things depending on if debug is on or not.		if($this->query_id = mysqli_query($query)) {			//no query id indicates failure			if(!$this->query_id) {				$this->debug_out("invalid query: $query");				return false;			} else {				return true;			}		} else {			if(empty($this->connection_id)) {				$this->debug_out("Not connected");			} else {				$this->debug_out("invalid query: $query");			}			return false;		}	}	public function get_row() {		mysqli_fetch_array($this-query_id);	}	public function count_rows() {		mysqli_num_rows($this->query_id);	}	/**	 * @param input scalar or array variable that needs to be sanitized for	 * input into the database. Failing to sanitize input will result in	 * problems with bobby tables re: http://xkcd.com/327/	 * @return Whatever the intut was sanitized. If the input was an array	 * expect an array back!	 */	public function sanitize ($input) {		//if this is an array, we need to recall the function again		//on the individual array items, then return the array with		//keys left alone, and items sanitized.		if($is_array($input)) {			//re-call this function on the indivitual items.			foreach ($input as $key => $item) {				$output[$key] = sanitize($i);			}		} else { //not an array			//we don't like this setting, so if it is on,			//lets strip slashes so we have something			//predictable to work with.			if(get_magic_quotes_gpc()) {				$input = stripslashes ($input);			}			$output = mysql_real_escape_string($input);		}		return $output;	}	/**	 * @param output Error output,	 * @return void	 */	private function debug_out($output) {		//we only output debug output if $debug is set to a		//value other then 0		if ($this->debug) {			if(empty($this->error)) {				printf("DB Error: %s", $output);			} else {				printf("DB Error: %s :: Message %s", $output, $this->error);			}			die ("Stopped.");		}	}}