<?php
class DB
{
var $handle;
var $host;
var $port;
var $name;
var $username;
var $password;
/*
Constructor
- Initialises class variables
*/
function __construct($host, $port, $name, $username, $password)
{
$this->host = $host;
$this->port = $port;
$this->name = $name;
$this->username = $username;
$this->password = $password;
}
/*
Destructor
Cleans up database connection!
*/
function __destruct()
{
if($this->handle)
mysql_close($this->handle);
}
/*
connectDatabase()
- Connects to mysql and selects a database.
- returns connection handle on success, redirects on failure
- @ surpresses errors otherwise setting headers fails
*/
function connectDatabase()
{
if($this->handle) return $this->handle;
$this->handle = @mysql_connect($this->host, $this->username, $this->password, true);
if(@mysql_select_db($this->name, $this->handle))
{
return $this->handle;
}
else
{
return false;
}
}
/*
* query
* Found on PHP comments
* Used with slight modifications
*
*/
function query($query)
{
$r = @mysql_query($query, $this->handle);
if(mysql_errno()) return false;
if(strtolower(substr($query, 0, 6)) != 'select' )
return array(mysql_affected_rows(), mysql_insert_id());
$count = @mysql_num_rows($r);
if(!$count) return 0;
if($count == 1)
{
$f = mysql_fetch_array($r);
mysql_free_result($r);
if(count($f) == 1)
{
list($key) = array_keys($f);
return $f[$key];
}
else
{
$all = array();
$all[] = $f;
return $all;
}
}
else
{
$all = array();
for( $i = 0; $i < $count; $i++ )
{
$f = mysql_fetch_array($r);
$all[] = $f;
}
}
mysql_free_result($r);
return $all;
}
}
?>
Bookmarks