Return to Snippet

Revision: 13720
at May 3, 2009 20:50 by sneaks


Initial Code
class Db {
	private $link;
	
	// Constructor method.
	// -------------------------------------------------
	function Db($host, $username, $password, $database) {
		$this->connect($host, $username, $password, $database);
	}
	
	function connect($host, $username, $password, $database) {
		$this->link = mysqli_connect($host,$username,$password,$database);
		$this->dbprefix = $dbprefix;
		
		if (!$this->link)
		exit('Connect failed: '.mysqli_connect_error());
		
		if (!mysqli_set_charset($this->link,'utf8'))
		exit('Error loading character set utf8: '.mysqli_error($this->link));
	}
	
	// Delete method.
	// -------------------------------------------------
	function delete($table, $id) {
		$sql = "DELETE FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'";
		return mysqli_query($this->link,$sql);
	}
	
	// Select method.
	// -------------------------------------------------
	function select($table, $id=null) {
		// No $id given, thus select all rows.
		if ( $id!="0" && empty($id))  {
			$sql = "SELECT * FROM `" . $table . "` ORDER BY `id`";
		}
		
		// grabs rows matching where clauses given
		elseif (is_array($id)) {
			$sql = "SELECT * FROM `" . $table . "` WHERE ";
			$first=true;
			foreach($id as $col=>$val) {
			if($first) $first=false;
			else $sql.=" AND ";
			$sql .= "`".Db::escape($col)."` = '" . Db::escape($val) . "'";
		}
		
		 // Grabs the row associated with the given $id.
		} else {
		$sql = "SELECT * FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'";
	}
	
	return mysqli_query($this->link,$sql);
	}
	
	// Update method.
	// -------------------------------------------------
	function update($table, $id) {
		$getColumns = mysqli_query($this->link,"SELECT * FROM " . $table);
		while($column = mysqli_fetch_field($getColumns)) {
			$column = $column->name;
			if (isset($_POST[$column])) {
				Utils::manipulateValues($column); // Manipulate certain values before inserting them into db.
				// This will be built up-on in the future.
				
				$fields[] = "`" . $column . "` = '" . htmlspecialchars($_POST[$column]) . "'";
			}
		}
		
		$sql = "UPDATE `" . $table . "` SET " . implode(", ", $fields) . " WHERE `id` = '" . $id . "'";
		return mysqli_query($this->link,$sql);
	}
	
	// Insert method.
	// -------------------------------------------------
	function insert($table) {
		$getColumns = mysqli_query($this->link,"SELECT * FROM " . $table);
		
		while($column = mysqli_fetch_field($getColumns)) {
			$column = $column->name;
			if (isset($_POST[$column])) {
				Utils::manipulateValues($column); // Manipulate certain values before inserting them into db.
				// This will be built up-on in the future.
				
				$fields[$column] = "'" . htmlspecialchars($_POST[$column]) . "'";
			}
		}
		
		$sql = "INSERT INTO `" . $table . "` (`" . implode("`, `", array_keys($fields)) . "`) VALUES (" . implode(", ", $fields) . ")";
		mysqli_query($this->link,$sql);
		return mysqli_insert_id($this->link);
	}
	
	// Normal query for custom needs.
	// NOTICE: When using this method, it is your job to assure user submitted-data is secure.
	// -------------------------------------------------
	function query($sql) {
		return mysqli_query($this->link,$sql);
	}
	
	function num_fields($result) {
		return mysqli_num_fields($result);
	}
	function fetch_field($result) {
		return mysqli_fetch_field($result);
	}
	
	function fetch_row($result) {
		return mysqli_fetch_row($result);
	}
	
	function num_rows($result) {
		return mysqli_num_rows($result);
	}
	
	function fetch_array($result) {
		return mysqli_fetch_array($result);
	}
	function fetch_assoc($result) {
		return mysqli_fetch_assoc($result);
	}
	function escape($string) {
		return mysqli_real_escape_string($this->link,$string);
	}
	
	// Check for tables existance.
	function table_exists($sector) {
		$getTables = mysqli_query($this->link,"SHOW TABLES");
		while($table = mysqli_fetch_array($getTables)) {
			if ($sector == $table[0]) {
				return true;
			}
		}
	}
	
	function show_columns($table, $column) {
		return mysqli_query($this->link,"SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
	}

}

$Db = new Db($settings['database']['databaseHost'], $settings['database']['databaseUsername'], $settings['database']['databasePassword'], $settings['database']['databaseName']);

Initial URL


Initial Description


Initial Title
mysqli db_wrapper

Initial Tags


Initial Language
PHP