<?php
/**
* DATABASE implements two interfaces, DBConsts, and DBFuncs.
*
* DBConsts contains connection-specific parameters.
*
* DBFuncs sets the base list of required functions.
*
* The constructor takes a database name as its only input and performs
* the connection and database selection procedures.
*
* Copyright 2009 Geoff Foley
* Distributed under the terms of the GNU General Public License
* http://www.gnu.org/licenses/gpl-3.0.txt
*/
require('dbconsts_int.php');
require('database_int.php');
class database implements DBConsts, DBFuncs {
// Constructor
public function __construct($dbn) {
$this->db_name = $dbn;
$this->db_link = mysql_connect(DBConsts::DB_SERVER, DBConsts::DB_USER, DBConsts::DB_PASSWORD);
if (!$this->db_link) die('Could not connect: ' . mysql_error());
$this->db_select = mysql_select_db($this->db_name,$this->db_link);
if(!$this->db_select) die('No database selected: ' . mysql_error());
}
// Interface Functions
public function affected_rows() {
return mysql_affected_rows($this->db_link);
}
public function clean_results() {
if($this->query_id != 0) mysql_freeresult($this->query_id);
}
public function close() {
if($this->db_link != 0) mysql_close($this->db_link);
}
public function delete($queryString) {
$this->query_id = @mysql_query($queryString,$this->db_link);
if (!$this->query_id) die('Invalid SQL Syntax: ' . mysql_error());
}
public function fetch_lengths() {
return mysql_fetch_lengths($this->query_id);
}
public function fetch_object() {
return mysql_fetch_object($this->query_id);
}
public function fetch_query($fetch=0) {
if($fetch == 0) {
$result = @mysql_fetch_assoc($this->query_id);
} else {
$result = @mysql_fetch_array($this->query_id);
}
if(!is_array($result))
return false;
foreach($result as $key => $val) {
$result[$key] = trim(htmlspecialchars($val));
}
return $result;
}
public function get_server() {
return mysql_get_server_info($this->db_link);
}
public function max_row($tablename,$field) {
$sql = "SELECT MAX({$field}) FROM {$tablename}";
$this->query($sql);
$result = @mysql_fetch_array($this->query_id);
return $result[0];
}
public function num_fields() {
return mysql_num_fields($this->query_id);
}
public function num_rows() {
return mysql_num_rows($this->query_id);
}
public function query($queryString) {
$this->query_id = @mysql_query($queryString,$this->db_link);
if (!$this->query_id) die('Invalid SQL Syntax Inside database::query ('.$queryString.'): ' . mysql_error());
}
}
?>