Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package Walmo.tv
- * @version 0.0.1
- * @author @_mvader
- * @copyright Walmo.tv 2010-11
- */
- /*
- * Database abstraction layer
- * Abstract class to connect and interact with Walmo's database
- */
- abstract class Database
- {
- private static $dbHost = 'localhost';
- private static $dbUser = 'root';
- private static $dbPass = 'root';
- private static $dbName = 'walmo';
- protected $query;
- protected $rows = array();
- private $conn;
- abstract public function get();
- abstract public function set();
- abstract public function update();
- abstract public function delete();
- /**
- * openConnection
- * Establishes database connection
- * @params: none
- */
- private function openConnection()
- {
- $this->conn = mysql_connect(self::$dbHost, self::$dbUser, self::$dbPass) or die("Error establishing database connection");
- $selected_db = mysql_select_db(self::$dbName, $this->conn) or die("Can't find the database " . $this->dbName);
- }
- /**
- * closeConnection
- * Closes the database connection
- * @params: none
- */
- private function closeConnection()
- {
- mysql_close($this->conn);
- }
- /**
- * query
- * This function executes a query without returning anything
- * @params: $q [string]
- */
- protected function query($q)
- {
- $this->openConnection();
- mysql_query($q, $this->conn) or die(mysql_error($this->conn));
- $this->closeConnection();
- }
- /**
- * getQuery
- * This function returns an associative array with the result received from a query
- * @params: $q [string]
- */
- protected function getQuery($q)
- {
- $this->openConnection();
- $result = mysql_query($q, $this->conn) or die("Query error");
- $this->closeConnection();
- return $this->fetch($result);
- }
- /**
- * fetch
- * Fetches into an array the data received from a query
- * @params: $f [object]
- */
- public function fetch($f)
- {
- return mysql_fetch_assoc($f);
- }
- /**
- * buildQuery
- * Builds a query with the given array
- * @params: $array [array], $mode [string]
- * Array structure:
- * $array = array(
- * ['TABLE'] => '',
- * ['FIELDS'] => array(),
- * ['VALUES'] => array(), // $array['VALUES'][i] = $array['FIELDS'][i] || Optional
- * ['WHERE'] => '', // optional
- * ['MISC'] => '', // optional
- * );
- */
- public function buildQuery(array $array, $mode)
- {
- if(is_array($array) && !empty($array) && !empty($mode))
- {
- switch ($mode)
- {
- /*
- * Build an update query
- */
- case 'update':
- $query = 'UPDATE ';
- foreach($array as $key => $value)
- {
- if($key == 'TABLE')
- {
- $query .= ' ' . $value . ' SET ';
- }
- if($key == 'FIELDS')
- {
- if(is_array($value))
- {
- $first = true;
- foreach($value as $field)
- {
- if($first)
- {
- $query .= $field;
- }
- else
- {
- $query .= ', ' . $field;
- }
- $first = false;
- }
- }
- else
- {
- $query .= $value;
- }
- }
- if($key == 'WHERE')
- {
- $query .= ' WHERE ' . $value;
- }
- if($key == 'MISC')
- {
- $query .= ' ' . $value;
- }
- }
- break;
- /*
- * Build an insert query
- */
- case 'insert':
- $query = 'INSERT INTO ';
- foreach($array as $key => $value)
- {
- if($key == 'TABLE')
- {
- $query .= $value . ' ';
- }
- if($key == 'FIELDS')
- {
- if(is_array($value))
- {
- $first = true;
- foreach($value as $field)
- {
- if($first)
- {
- $query .= "(" . $field;
- }
- else
- {
- $query .= ", " . $field;
- }
- $first = false;
- }
- }
- else
- {
- $query .= $value . ' ';
- }
- }
- if($key == 'VALUES')
- {
- if(is_array($value))
- {
- $first = true;
- foreach($value as $field)
- {
- if($first)
- {
- $query .= ") VALUES ('" . $field;
- }
- else
- {
- $query .= "', '" . $field;
- }
- $first = false;
- }
- $query .= "')";
- }
- else
- {
- $query .= ") VALUES ('" . $value . "')";
- }
- }
- }
- break;
- /*
- * Build a select query
- */
- case 'select':
- $query = 'SELECT ';
- foreach($array as $key => $value)
- {
- if($key == 'TABLE')
- {
- $query .= ' FROM ' . $value . ' ';
- }
- if($key == 'FIELDS')
- {
- if(is_array($value))
- {
- $first = true;
- foreach($value as $field)
- {
- if($first)
- {
- $query .= $field;
- }
- else
- {
- $query .= ', ' . $field;
- }
- $first = false;
- }
- }
- else
- {
- $query .= $value . ' ';
- }
- }
- if($key == 'WHERE')
- {
- $query .= ' WHERE ' . $value;
- }
- if($key == 'MISC')
- {
- $query .= ' ' . $value;
- }
- }
- break;
- /*
- * Build a delete query
- */
- case 'delete':
- $query = 'DELETE FROM ';
- foreach($array as $key => $value)
- {
- if($key == 'TABLE')
- {
- $query .= $value;
- }
- if($key == 'WHERE')
- {
- $query .= ' WHERE ' . $value;
- }
- }
- break;
- default:
- trigger_error('Error @ build_query() => Mode not specified.');
- }
- return $query;
- }
- else
- {
- trigger_error('Error @ build_query() => Data error.');
- }
- }
- /**
- * cleanString
- * Cleans a string and removes illegal characters
- * @params: $var [string]
- */
- public function cleanString($var)
- {
- $var = preg_replace("[^A-Za-z0-9]", "", $var);
- $var = str_replace(" ", "-", $var);
- $var = strtolower($var);
- return $var;
- }
- /**
- * affectedRows
- * Returns the number of affected rows by the previous query executed
- * @params: none
- */
- public function affectedRows()
- {
- return ($this->conn) ? @mysql_affected_rows($this->conn) : false;
- }
- /**
- * freeResult
- * Releases the memory used by a query previously executed
- * @params: $query [object]
- */
- public function freeResult($query)
- {
- mysql_free_result($query);
- }
- /**
- * sanitize
- * Cleans a variable before storing it in the databse
- * @params: $var [string]
- * Note: use $var = (int) $var; for integer values
- */
- public function sanitize($var)
- {
- $this->openConnection();
- $var = mysql_real_escape_string($var,$this->conn);
- $this->closeConnection();
- return $var;
- }
- }
Add Comment
Please, Sign In to add comment