<?
#####
# Classe database - Controle de Banco de Dados
# Autor: Filipe Kiss
# Release: 1.0.2
# Último release: 2009-03-27
#####
Class database
{
var $dbh, $lastResult, $tempCon;
function database($u, $s, $b, $h)
{
$this->__construct($u, $s, $b, $h);
}
function __construct($usuario, $senha, $banco, $host)
{
$this->dbh = mysql_connect($host, $usuario, $senha) or die ("Erro Ao Conectar com o Servidor ".$host." com o usuário ".$usuario);
mysql_select_db($banco, $this->dbh) or die ("Servidor Conectado com Sucesso. Erro ao tentar acessar o banco ".$banco." com o usuário ".$usuario);
}
function connect()
{
if(empty($this->dbh))
{
mysql_connect($host, $usuario, $senha) or die ("Erro Ao Conectar com o Servidor ".$host." com o usuário ".$usuario);
mysql_select_db($banco) or die ("Servidor Conectado com Sucesso. Erro ao tentar acessar o banco ".$banco." com o usuário ".$usuario);
$this->tempCon = true;
}
}
function disconnect()
{
if($this->tempCon == true)
{
mysql_close();
$this->tempCon = false;
}
}
function query($query)
{
$this->connect();
$return_val = 0;
$this->result = mysql_query($query, $this->dbh) or die(
"<h3>An Error Ocurred</h3>
<div>".mysql_error()."</div><br />".$query
);
if(mysql_error($this->dbh) != '')
$this->result = false;
$this->lastResult = $this->result;
$this->disconnect();
return $this->result;
}
function get_var($query)
{
$this->result = $this->query($query);
if($this->result)
{
if(mysql_num_rows($this->result) != 0)
$this->return_value = mysql_result($this->result, 0);
else
$this->return_value = 0;
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
function get_results($query)
{
$this->result = $this->query($query);
if($this->result)
{
$this->return_value = array();
if(mysql_num_rows($this->result) != 0)
{
while($this->data = mysql_fetch_assoc($this->result))
{
$this->return_value[] = $this->data;
}
}
else
{
$this->return_value = 0;
}
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
function get_row($query)
{
$this->result = $this->query($query);
if($this->result)
{
if(mysql_num_rows($this->result) != 0)
{
while($this->data = mysql_fetch_assoc($this->result))
{
if(!$this->tempData)
{
$this->tempData = $this->data;
}
}
$this->return_value = $this->tempData;
unset($this->tempData);
}
else
{
$this->return_value = 0;
}
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
}
$db_server = 'localhost'; //Geralmente é localhost. caso não funcione, consulte seu provedor
$db_name = 'banco'; //Nome do Banco de Dados
$db_user = 'user'; //Nome do Usuário
$db_passwd = 'senha'; //Senha
$mdb = new database($db_user, $db_passwd, $db_name, $db_server);
?>