<?php
/**
* This file defines the OFFL_DBObject class
*
* @package offl
* @author Stephen Rochelle <hide@address.com>
*/
if (strtr(__FILE__, "\\", "/") == $_SERVER["SCRIPT_FILENAME"])
{ die ("Cannot access file directly!"); }
require_once($DOC_ROOT . "/lib/config.php");
require_once($DOC_ROOT . "/lib/classes/offl_stub.php");
define("SQL_BOTH", 0);
define("SQL_NUM", 1);
define("SQL_ASSOC", 2);
/**
* Class for abstracting database connections. Ought to expand to include all SQL calls.
*
* {@inheritdoc}
* @package offl
*/
class OFFL_DBObject extends OFFL_Stub
{
/**
* @var mixed MySQL database connection
*/
var $_conn;
/**
* @var array Error message array
*/
var $_emsg;
/**
* @var mixed MySQL Query Result
*/
var $_result;
/**
* Constructor, opens a database connection
*
* @see openDB()
*/
function OFFL_DBObject () {
$this->openDB();
}
/**
* Opens a database connection with values from fflconfig.php
*/
function openDB()
{
$host = DB_HOST;
if (defined("DB_PORT"))
{ $host .= ":" . DB_PORT; }
$this->_conn = mysql_pconnect($host, DB_USER, DB_PASSWD )
or die("Could not connect to database: " . mysql_error());
mysql_select_db(DB_NAME, $this->_conn);
}
/**
* Closes the database connection
*/
function closeDB()
{
mysql_close($this->_conn);
}
/**
* Returns the MySQL connection object
*
* @return mixed MySQL connection object
*/
function getConn()
{
return $this->_conn;
}
/**
* Returns the array of error messages
*
* @return array
*/
function getEMsg()
{
return $this->_emsg;
}
/**
* Wrapper for SQL queries. For ease of portability.
*
*
* @param string $query_string The query to be passed to the database
* @return mixed Returns the result of the query string
*/
function SQLQuery($query_string)
{
$this->_result = mysql_query($query_string, $this->_conn);
return $this->_result;
}
/**
* Wrapper for SQL errors. For ease of portability.
*
*
* @param boolean $number Optional: set TRUE to return error number instead of error message
* @return mixed Returns the last SQL error / error number
*/
function SQLError($number = FALSE)
{
if ($number)
{ return mysql_errno(); }
return mysql_error();
}
/**
* Wrapper for SQL autoinsert values. For ease of portability.
*
* @return int Returns the last autoinserted index value from this object's connection
*/
function SQLInsertID()
{
return mysql_insert_id($this->_conn);
}
/**
* Wrapper for SQL result returns. For ease of portability.
*
* @param integer $row Target row from {@link $_result}
* @param string $field Target field from {@link $_result}
* @return mixed Returns the relevant result data
*/
function SQLResult($row, $field)
{
return mysql_result($this->_result, $row, $field);
}
/**
* Wrapper for SQL result row returns. For ease of portability
*
* Arrays may be indexed numerically, associatively, or both via constants SQL_NUM, SQL_ASSOC, or SQL_BOTH (the default)
*
* @param int $type Optional: Specifies type of array indexing (default is both numeric and associative)
* @return mixed Boolean FALSE on failure, otherwise an array as specified
*/
function SQLRow($type=SQL_BOTH)
{
if (!is_resource($this->_result))
{ return FALSE; }
switch ($type) {
case SQL_NUM:
return mysql_fetch_array($this->_result, MYSQL_NUM);
case SQL_ASSOC:
return mysql_fetch_array($this->_result, MYSQL_ASSOC);
default:
return mysql_fetch_array($this->_result, MYSQL_BOTH);
}
}
/**
* Wrapper for SQL free result calls. For ease of portability.
*
* @return boolean Returns success of call
*/
function SQLFreeResult()
{
if (!is_resource($this->_result))
{ return FALSE; }
mysql_free_result($this->_result);
return TRUE;
}
/**
* Wrapper for SQL number of result rows. For ease of portability.
*
* @return integer Number of returned rows.
*/
function SQLNumRows()
{
return mysql_num_rows($this->_result);
}
}
?>