<?
/******************************************************************************
* Name : Database
* Description : Class for database connection
*****************************************************************************/
class DBConnection
{
var $m_host; // hostname
var $m_login; // login (user name)
var $m_password; // password
var $m_dbcn; // database connection
var $m_query; // query
function DBConnection()
{
// Set variables for connection
$this->m_host = "localhost";
$this->m_login = "httpd";
$this->m_password = "pass";
$this->m_database = "wavewatcher3";
}
// Connects to the database
function Connect()
{
// Connect to the database
$this->m_dbcn = mysql_connect($this->m_host, $this->m_login, $this->m_password) or die("Could not connect to the database!");
// Set database
mysql_select_db("wavewatcher3", $this->m_dbcn) or die("Wavewatcher3 doesn't exist!");
// Return database connection
return $this->m_dbcn;
}
// Disconnects from the database, this isn't really nescessary since mysql closes it after a query
function Disconnect()
{
if (IsSet($m_dbcn))
mysql_close($m_dbcn) or die("Oups... Could not close database connection");
}
// Returns a reference to the database connection
function GetConnection()
{
return $this->m_dbcn;
}
// Executes a query and returns the recordset
function Query($query, $lock_array = FALSE)
{
/**********************************************************************
* Parameters
* ----------
* $query - Query to be executed
* $lock_array - An array of tables and lockingmodes
*
* Example:
* Query("SELECT * FROM Users", array("users", "READ", "nodes", "WRITE"));
**********************************************************************/
// lock-query variable
$strQuery = "";
// resource identifier to return
$resId = FALSE;
// Is the second parameter an array => lock_array
if (gettype($lock_array) == "array")
{
$strQuery = "LOCK TABLES ";
// Step through list and generate the query
for ($i = 0; IsSet($lock_array[$i]); $i++)
{
$strQuery = "$strQuery $lock_array[$i]";
// Do we need any comma?
if (IsSet($lock_array[$i+1]) && $i%2 == 1)
$strQuery= "$strQuery,";
}
// Execute query and return the resource identifier
if(mysql_query($strQuery, $this->m_dbcn))
$resId = mysql_query($query, $this->m_dbcn);
// unlock the table(s)
mysql_query("UNLOCK TABLES", $this->m_dbcn);
return $resId;
}
// Execute query without locks
return mysql_query($query, $this->m_dbcn);
}
// Fetch row and return it
function FetchRow($result)
{
return mysql_fetch_row($result);
}
// Move to a specified row in a resultset
function DataSeek($result, $row)
{
if (!$result or $row < 0)
return FALSE;
return mysql_data_seek($result, $row);
}
}
?>