Scripts
Database class
Deze classe heb ik geschreven voor mijn Opleiding bij Eduvision. De PHP Masterclass. Ik post hem hier voornamelijk om te kijken of er nog op en/of aanmerkingen zijn op dit script. Als mensen hiervan kunnen leren is dat alleen maar meegenomen.
database-class
<?php
/**
* clsDatabase
*
* @package PHP Masterclass
* @author Phoenix
* @copyright 2007
* @version $Id$
* @access public
*/
error_reporting(E_ALL | E_STRICT);
require_once("classes/main_inc.php");
class Database
{
private $m_sName;
private $m_sHost;
private $m_sUser;
private $m_sPassword;
private $m_aConnect;
private $m_sError;
private $p_sRoot;
private $m_bConnected;
/**
* clsDatabase::__construct()
*
* @return@var
*/
function __construct()
{
$this->m_sName = "";
$this->m_sHost = "";
$this->m_sUser = "";
$this->m_sPassword = "";
$this->m_sError = false;
$this->m_aConnect = null;
$this->p_sRoot = "";
$this->m_bConnected = false;
}
/**
* clsDatabase::__destruct()
*
* @return
*/
function __destruct()
{
if ($this->m_aConnect) {
mysql_close($this->m_aConnect);
$this->m_aConnect = null;
}
}
/**
* clsDatabase::__set()
*
* @param mixed $p_sVar
* @param mixed $p_sValue
* @return
*/
function __set($p_sVar, $p_sValue)
{
switch ($p_sVar) {
case "sName":
if ($p_sValue != "") {
$this->m_sName = $p_sValue;
} else {
echo "<b>Database</b> | Connect() | No valid database name<br />";
$this->m_sError = true;
}
break;
case "sUser":
if ($p_sValue != "") {
$this->m_sUser = $p_sValue;
} else {
echo "<b>Database</b> | Connect() | No valid username<br />";
$this->m_sError = true;
}
break;
case "sHost":
if ($p_sValue != "") {
$this->m_sHost = $p_sValue;
} else {
echo "<b>Database</b> | Connect() | No valid host<br />";
$this->m_sError = true;
}
break;
case "sPassword":
if ($p_sValue != "") {
$this->m_sPassword = $p_sValue;
} else {
echo "<b>Database</b> | Connect() | No valid password<br />";
$this->m_sError = true;
}
break;
case "sRoot":
if ($p_sValue != "") {
$this->p_sRoot = $p_sValue;
} else {
$this->p_sRoot = "";
}
break;
}
}
/**
* clsDatabase::__get()
*
* @param mixed $p_sVar
* @return
*/
function __get($p_sVar)
{
$vResult = false;
switch ($p_sVar) {
case "sHost":
$vResult = $this->m_sHost;
break;
case "sName":
$vResult = $this->m_sName;
break;
case "sUser":
$vResult = $this->m_sUser;
break;
case "sPassword":
$vResult = $this->m_sPassword;
break;
case "sRoot":
$vResult = $this->p_sRoot;
break;
}
return $vResult;
}
/**
* Database connectie maken
* @var $p_sHost - de host van je db server (meestal localhost)
* @var $p_sName - de naam van je database
* @var $p_sUser - de usernaam van je db server
* @var $p_sPassword - de password van je db server (deze mag leeg zijn, maar of dat veilig is ;) )
**/
/**
* clsDatabase::Connect()
*
* @param mixed $p_sHost
* @param mixed $p_sName
* @param mixed $p_sUser
* @param mixed $p_sPassword
* @return
*/
public function Connect($p_sHost, $p_sName, $p_sUser, $p_sPassword)
{
if ($this->m_aConnect == null)
{
if ($this->m_sError == true) {
throw new MyException ('<b>Database</b> | Connect() | 1 of more invalid parameters!');
} else {
$this->m_aConnect = mysql_connect($p_sHost, $p_sUser, $p_sPassword);
mysql_select_db($p_sName, $this->m_aConnect);
}
} else {
throw new MyException ('<b>Database</b> | Connect() | Close database connection');
}
}
/**
* Database::IsConnected()
*
* @return
*/
public function IsConnected () {
if ($this->m_aConnect == null)
{
throw new MyException ('<b>Database</b> | IsConnected() | No database connection, Could not execute query\'s!');
}
RETURN NULL;
}
/**
* Database::Query()
*
* @param mixed $m_sQuery
* @return
*/
public function Query ($m_sQuery) {
$this->IsConnected();
$m_sResult = @mysql_query($m_sQuery);
RETURN $m_sResult;
}
/**
* Database::FetchArray()
*
* @param mixed $m_sQuery
* @return
*/
public function FetchArray ($m_sQuery) {
$this->IsConnected();
$m_sResult = @mysql_fetch_array($m_sQuery);
RETURN $m_sResult;
}
/**
* Database::FetchAssoc()
*
* @param mixed $m_sQuery
* @return
*/
public function FetchAssoc ($m_sQuery) {
$this->IsConnected();
$m_sResult = @mysql_fetch_assoc($m_sQuery);
RETURN $m_sResult;
}
/**
* Database::FetchObject()
*
* @param mixed $m_sQuery
* @return
*/
public function FetchObject ($m_sQuery) {
$this->IsConnected();
$m_sResult = @mysql_fetch_object($m_sQuery);
RETURN $m_sResult;
}
/**
* Database::FetchRow()
*
* @param mixed $m_sQuery
* @return
*/
public function FetchRow ($m_sQuery) {
$this->IsConnected();
$m_sResult = @mysql_fetch_row($m_sQuery);
RETURN $m_sResult;
}
/**
* Database::FreeResult()
*
* @return
*/
public function FreeResult()
{
@mysql_free_result();
}
/**
* Database::NumRows()
*
* @param mixed $m_sQuery
* @return
*/
public function NumRows($m_sQuery)
{
$this->IsConnected();
$m_sResult = @mysql_num_rows($m_sQuery);
if ($m_sResult === FALSE) {
throw new MyException ('<b>Database</b> | NumRows() | Could not count rows');
}
RETURN $m_sResult;
}
}
?>
Reacties
0