Har oprettet en trå som denne før, men er åbenbart lidt tungnem og har lige brug for at slå det her helt på plads.
Er denne klasse controller eller model, eller overhovedet ikke MVC?
- <?php
- /**
- * Database management
- * @version
- */
- class database
- {
- /**
- * Allows multiple connections
- */
- private $connections = array();
-
- /**
- * Active connection
- */
- private $activeConnection = 0;
-
- /**
- * Query cache
- */
- private $queryCache = array();
-
- /**
- * Data cache
- */
- private $dataCache = array();
-
- /**
- * The last query
- */
- private $lastQuery;
-
- public function __construct()
- {
- }
-
- /**
- * Create a new connection
- * @param String database hostname
- * @param String database username
- * @param String database password
- * @param String database name
- * @return int connection id
- */
- public function newConnection($host, $user, $password, $database)
- {
- $this->connections[] = mysql_connect($host, $user, $password);
- $connection_id = count($this->connections)-1;
- mysql_select_db($database, $this->connections[$connection_id]);
-
- if(!$this->connections[$connection_id])
- trigger_error("Error connecting to host: " . mysql_error(), E_USER_ERROR);
-
- return $connection_id;
- }
-
- /**
- * Close active connection
- * @return void
- */
- public function closeConnection()
- {
- mysql_close($this->connections[$this->activeConnection]);
- }
-
- /**
- * Set an active connection
- * @param int active connection id
- * @return void
- */
- public function setActiveConnection($new)
- {
- $this->activeConnection = $new;
- }
-
- /**
- * Store a query in the query cache
- * @param String query string
- * @return int cache-id of query
- */
- public function cacheQuery($queryString)
- {
- $result = mysql_query($queryString, $this->connections[$this->activeConnection]);
- if(!$result)
- {
- trigger_error("Error executing and caching query: " . mysql_error(), E_USER_ERROR);
- return -1;
- }
- else
- {
- $this->queryCache[] = $result;
- return count($this->queryCache)-1;
- }
- }
-
- /**
- * Number of rows from cache
- * @param int cache-id of query
- * @return int number of rows
- */
- public function numRowsFromCache($cache_id)
- {
- return mysql_num_rows($this->queryCache[$cache_id]);
- }
-
- /**
- * Get rows from cached query
- * @param int cache-id of query
- * @return array the rows
- */
- public function resultsFromCache($cache_id, $method = "assoc")
- {
- if($method == "assoc")
- return mysql_fetch_assoc($this->queryCache[$cache_id]);
- else
- return mysql_fetch_array($this->queryCache[$cache_id]);
- }
-
- /**
- * Store data in cache
- * @param array data
- * @return int cache-id of query
- */
- public function cacheData($data)
- {
- $this->dataCache[] = $data;
- return count(dataCache)-1;
- }
-
- /**
- * Get data from data cache
- * @param int cache-id
- * @return array data
- */
- public function dataFromCache($cache_id)
- {
- return $this->dataCache[$cache_id];
- }
-
- /**
- * Delete records
- * @param String table name
- * @param String condition
- * @param int number of rows to be removed
- * @return void
- */
- public function deleteRecords($table, $condition, $limit)
- {
- $limit = ($limit == '') ? "" : "LIMIT " . $limit;
- $delete = "DELETE FROM " .$table. " WHERE " .$condition. " " . $limit;
- $this->executeQuery($delete);
- }
-
- /**
- * Update records
- * @param String table name
- * @param array changes field => value
- * @param String condition
- * @return bool
- */
- public function updateRecords($table, $changes, $condition)
- {
- $update = "UPDATE " .$table. " SET ";
- foreach($changes as $field => $value)
- {
- $update .= $field . " = '" . $value . "',";
- }
-
- $update = substr($update, 0, -1);
- if($condition != "")
- $update .= " WHERE " . $condition;
- $this->executeQuery($update);
-
- return true;
- }
-
- /**
- * Insert records
- * @param String table name
- * @param array data field => value
- * @return bool
- */
- public function insertRecords($table, $data)
- {
- $fields = "";
- $values = "";
-
- foreach($data as $f => $v)
- {
- $fields .= $f.",";
- $values .= (is_numeric($v) && (intval($v) == $v)) ? $v."," : "'".$v."',";
- }
-
- $fields = substr($fields, 0, -1);
- $values = substr($values, 0, -1);
-
- $insert = "INSERT INTO " .$table." (" .$fields.") VALUES (".$values.")";
- $this->executeQuery($insert);
-
- return true;
- }
-
- /**
- * Execute a query
- * @param String query string
- * @return void
- */
- public function executeQuery($queryString)
- {
- $result = mysql_query($queryString, $this->connections[$this->activeConnection]);
- if(!$result)
- trigger_error("Error executing query: " . mysql_error(), E_USER_ERROR);
- else
- $this->last = $result;
- }
-
- /**
- * Get rows of latest query
- * @return array
- */
- public function getRows($method = "assoc")
- {
- if($method == "assoc")
- return mysql_fetch_assoc($this->last);
- else
- return mysql_fetch_array($this->last);
- }
-
- /**
- * Gets number of affected rows from the latest query
- * @return int number of affected rows
- */
- public function affectedRows()
- {
- return mysql_affected_rows($this->connections[$this->activeConnection]);
- }
-
- /**
- * Sanitize data
- * @param String data
- * @return String sanitized data
- */
- public function sanitizeData($data)
- {
- return mysql_real_escape_string($data);
- }
-
- /**
- * Deconstruct
- * Close all connections
- */
- public function __deconstruct()
- {
- foreach($this->connections as $connection)
- mysql_close($connection);
- }
- }
- ?>