Created
April 28, 2013 22:53
-
-
Save uppfinnarjohnny/5478739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* File Name: Database.php | |
* Date: November 18, 2008 | |
* Author: Angelo Rodrigues | |
* Description: Contains database connection, result | |
* Management functions, input validation | |
* | |
* All functions return true if completed | |
* successfully and false if an error | |
* occurred | |
* | |
*/ | |
class Database | |
{ | |
/* | |
* Edit the following variables | |
*/ | |
private $db_host = 'localhost'; // Database Host | |
private $db_user = 'root'; // Username | |
private $db_pass = 'root'; // Password | |
private $db_name = 'blog'; // Database | |
/* | |
* End edit | |
*/ | |
private $con = false; // Checks to see if the connection is active | |
private $result = array(); // Results that are returned from the query | |
/* | |
* Connects to the database, only one connection | |
* allowed | |
*/ | |
public function connect() | |
{ | |
if($this->con) | |
return true; | |
$myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); | |
if( ! $myconn) | |
return false; | |
$seldb = @mysql_select_db($this->db_name, $myconn); | |
$this->con = (bool) $seldb; | |
return (bool) $seldb; | |
} | |
/* | |
* Changes the new database, sets all current results | |
* to null | |
*/ | |
public function setDatabase($name) | |
{ | |
if( ! $this->con || ! mysql_close()) | |
return false; | |
$this->con = false; | |
$this->results = null; | |
$this->db_name = $name; | |
$this->connect(); | |
} | |
/* | |
* Checks to see if the table exists when performing | |
* queries | |
*/ | |
private function tableExists($table) | |
{ | |
return mysql_num_rows(mysql_query("SHOW TABLES FROM {$this->db_name} LIKE \"{$table}\"") == 1; | |
} | |
/* | |
* Selects information from the database. | |
* Required: table (the name of the table) | |
* Optional: rows (the columns requested, separated by commas) | |
* where (column = value as a string) | |
* order (column DIRECTION as a string) | |
*/ | |
public function select($table, $rows = '*', $where = null, $order = null) | |
{ | |
$where = $where ? " WHERE {$where}" : ''; | |
$order = $order ? " ORDER BY {$order}" : ''; | |
$q = "SELECT {$rows} FROM {$table}{$where}{$order}"; | |
$query = @mysql_query($q); | |
if( ! $query) | |
return false; | |
$this->result = array(); | |
while($row = mysql_fetch_assoc($query)) | |
$this->result[] = $row; | |
$this->numResults = count($this->result); | |
return true; | |
} | |
/* | |
* Insert values into the table | |
* Required: table (the name of the table) | |
* values (the values to be inserted) | |
* Optional: rows (if values don't match the number of rows) | |
*/ | |
public function insert($table,$values,$rows = null) | |
{ | |
if( ! $this->tableExists($table)) | |
return false; | |
$insert = 'INSERT INTO '.$table; | |
if($rows != null) | |
$insert .= ' ('.$rows.')'; | |
$values = array_map(array($this, 'quote_strings'), $values); | |
$values = implode(', ', $values); | |
$insert .= ' VALUES ('.$values.')'; | |
return (bool) mysql_query($insert); | |
} | |
/* | |
* Deletes table or records where condition is true | |
* Required: table (the name of the table) | |
* Optional: where (condition [column = value]) | |
*/ | |
public function delete($table, $where = null) | |
{ | |
if( ! $this->tableExists($table)) | |
return false; | |
$delete = is_null($where) ? "DELETE {$table}" : "DELETE FROM {$table} WHERE {$where}"; | |
return (bool) @mysql_query($delete); | |
} | |
/* | |
* Updates the database with the values sent | |
* Required: table (the name of the table to be updated | |
* rows (the rows/values in a key/value array | |
* where (the row/condition in an array (row,condition) ) | |
*/ | |
public function update($table,$rows,$where) | |
{ | |
if( ! $this->tableExists($table)) | |
return false; | |
$wheres = array(); | |
foreach($where as $column => $criteria) | |
$wheres[] = "{$column} = \"{$criteria}\""; | |
$where = implode(' AND ', $wheres); | |
$values = array(); | |
foreach($rows as $column => $value) | |
$values[] = $column.' = '.$this->quote_strings($value); | |
$values = implode(', ', $values); | |
return (bool) mysql_query("UPDATE {$table} SET {$values} WHERE {$where}"); | |
} | |
protected function quote_strings($value) { | |
return is_string($value) ? "\"{$value}\"" : $value; | |
} | |
/* | |
* Returns the result set | |
*/ | |
public function getResult() | |
{ | |
return $this->result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment