Created
April 13, 2010 19:18
-
-
Save iamjonbradley/364979 to your computer and use it in GitHub Desktop.
Database Abstraction Class
This file contains 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 | |
class Database { | |
var $hostname = ''; | |
var $username = ''; | |
var $password = ''; | |
var $database = ''; | |
var $link = ''; | |
var $i = 0; | |
function __construct(&$model) { | |
$this->model = $model; | |
$this->__connect(); | |
} | |
function __connect() { | |
mysql_connect($this->hostname, $this->username, $this->password); | |
mysql_select_db($this->database); | |
} | |
function find($type, $options = array()) { | |
switch ($type): | |
case 'all': return self::all($options); break; | |
case 'first': return self::first($options); break; | |
endswitch; | |
} | |
function first($options = array()) { | |
// set fields | |
$sql = "SELECT * FROM ". $this->model ." "; | |
// set conditions | |
if (isset($options['conditions'])): | |
$where = 'WHERE '; | |
foreach ($options['conditions'] as $key => $value): | |
$where .= $key ." = '". $value ."' AND "; | |
endforeach; | |
$sql .= substr($where, 0, -5); | |
endif; | |
$sql .= " LIMIT 1"; | |
$query = mysql_query($sql); | |
return mysql_fetch_assoc($query); | |
} | |
function all($options = array()) { | |
// set fields | |
if (isset($options['fields'])): | |
$fields = ''; | |
foreach ($options['fields'] as $key => $value): | |
$fields .= $value .', '; | |
endforeach; | |
$sql = sprintf("SELECT %s FROM ". $this->model ." ", substr($fields, 0, -2)); | |
else: | |
$sql = "SELECT * FROM ". $this->model ." "; | |
endif; | |
// set conditions | |
if (isset($options['conditions'])): | |
$where = 'WHERE '; | |
foreach ($options['conditions'] as $key => $value): | |
$where .= $key ." = '". $value ."', "; | |
endforeach; | |
$sql .= substr($where, 0, -2); | |
endif; | |
// set result | |
$query = mysql_query($sql); | |
$i = 0; | |
while ($row = mysql_fetch_assoc($query)): | |
$keys = array_keys($row); | |
for ($a = 0; $a<count($keys); $a++): | |
$result[$i][$keys[$a]] = $row[$keys[$a]]; | |
endfor; | |
$i++; | |
endwhile; | |
return $result; | |
} | |
function delete($id) { | |
$sql = "DELETE FROM ". $this->model ." WHERE id =". self::clean($id); | |
mysql_query($sql); | |
} | |
function save($post = array()) { | |
$sql = "INSERT INTO ". $this->model ." "; | |
// set fields | |
$fields = ''; | |
$keys = array_keys($post); | |
foreach ($keys as $key => $value): | |
$fields .= $value .','; | |
endforeach; | |
$sql .= '('. substr($fields, 0, -1) .') '; | |
// set values | |
$sql .= 'VALUES '; | |
$values = ''; | |
foreach ($post as $key => $value): | |
$values .= '"'. $value .'",'; | |
endforeach; | |
$sql .= '('. substr($values, 0, -1) .') '; | |
// execute query | |
mysql_query($sql); | |
} | |
function update($post = array()) { | |
$sql = "UPDATE ". $this->model ." SET "; | |
// set update fields | |
$fields = ''; | |
foreach ($post as $key => $value): | |
if ($key != 'id') $fields .= $key .' = "'. self::clean($value) .'", '; | |
endforeach; | |
// set where | |
$sql .= substr($fields, 0, -2) ." WHERE id = ". self::clean($post['id']); | |
// execute query | |
mysql_query($sql); | |
} | |
function clean($string) { | |
return mysql_real_escape_string($string); | |
} | |
} | |
?> |
This file contains 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 | |
include('../inc/classes/dbo.php'); | |
class Pages extends Database { | |
var $model = 'pages'; | |
function __construct() { | |
parent::__connect(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment