<?PHP
##########################################################
#
# PHPMAFIA - http://www.phpmafia.com.br
# Vesão : 0.7 BETA
# Programador : Marcelo Costa
# Email : phpmafia at yahoo dot com dot br
# Data :13/12/2002
# Título : class_innodb.php
# Descrição : classe para banco de dados mysql com funções para tabelas innodb
# Modificado : 13/02/2003
# Últ Alteração : 06/02/2003 - modificado as funções getdbresult() e othersql()
# 13/02/2003 - adicionada a funcao getdbrecord($rs)
#
############################################################
#
# Como usar esta classe
#
# 1 - SELECT
#
# carregue a classe com incluse ou require
#
# require("./class_innodb.php");
#
# Instancie o Objeto construtor da classe
#
# $Objeto= new innodb();
#
# Passe a query para a classe
#
# $SQL="SELECT * FROM tabela";
# $Objeto->setsql($SQL);
#
# Verifica o numero de resultados e pega o array de resultados
#
#
# if($Objeto->getnumberrows()>0) // Verifica o numero de resultados
# {
# foreach ($Objeto->getdbarray() as $linha) // percorre o array multidimensional
# {
# $campo1=$linha["nomedocampo1"];
# $campo2=$linha["nomedocampo2"];
#
# echo "campo1=".$campo1." <br> \r\n";
# echo "campo2=".$campo2." <br> \r\n";
# }
# }else{
# echo "A ".$SQL." query nao retornou nenhum resultado";
# }
#
#
#
# 2 - INSERT , UPDATE ou DELETE , querys que nao retornam resultados
#
# carregue a classe com incluse ou require
#
# require("./class_innodb.php");
#
# Instancie o Objeto construtor da classe
#
# $Objeto= new innodb();
#
# Passe a query para a classe
#
# $Objeto->setsql("INSERT INTO tabela VALUES ($valor1,'$valor2')");
#
# Efetua o insert, update ou delete
#
# $Objeto->othersql();
#
# Pega o ultimo id auto increment
#
# $Objeto->getlastid(); // somente para insert
#
#
# Pega o mumero de linhas afetadas
#
# $Objeto->affected(); // somente insert, update e delete com clausula where (ver manual)
#
# 3 - Transação
#
# basta usar a funcao $Objeto->begin() antes e $Objeto->commit() após efetuar as querys , mas pode ser interessante usar somente
# usar $Objeto->setsql() e $Objeto->othersql() concatenando as querys
#
$DB="DATABASE";
$HOST="localhost";
$USER="USUARIO";
$PASSWORD="SENHA";
$TRUE = 1 ;
$FALSE = 0 ;
class innodb
{
var $dbhost;
var $db;
var $dbuser;
var $dbpassword;
var $sql;
var $numberrows;
var $dbopenstatus;
var $dbconnection;
var $dbresults = array(); //Array de ressultados da query
# CONSTROI A CONEXAO COM O BANCO
function innodb()
{
global $HOST, $DB, $USER, $PASSWORD;
global $TRUE, $FALSE;
$this->sethost($HOST);
$this->setdb($DB);
$this->setdbuser($USER);
$this->setdbpassword($PASSWORD);
$this->setdbconnection($FALSE);
}
# RETORNA O HOSTNAME
function gethost()
{
return $this->dbhost;
}
# SETA O HOSTNAME
function sethost($req_host)
{
$this->dbhost = $req_host;
}
# RETORNA O BANCO DE DADOS DA CONEXAO
function getdb()
{
return $this->db;
}
# SETA O BANCO DE DADOS DA CONEXAO
function setdb($req_db)
{
$this->db = $req_db;
}
# RETORNA O USUARIO DO BANCO
function getdbuser()
{
return $this->dbuser;
}
# SETA O USUARIO DO BANCO DE DADOS
function setdbuser($req_user)
{
$this->dbuser = $req_user;
}
# RETORNA A SENHA DE CONEXAO DO BANCO
function getdbpassword()
{
return $this->dbpassword;
}
# FUNCAO QUE SETA A SENHA DE CONEXAO DO BANCO
function setdbpassword($req_password)
{
$this->dbpassword = $req_password;
}
# SETA UMA CONEXAO
function setdbconnection($req_dbconnection)
{
$this->dbconnection = $req_dbconnection;
}
# RETORNA A STRING DE CONEXAO
function getdbconnection()
{
return $this->dbconnection;
}
# RETORNA A QUERY
function getsql()
{
return $this->sql;
}
# SETA A QUERY QUE VAI SER EXECUTADA
function setsql($req_sql)
{
$this->sql = $req_sql;
}
# PEGA O NUMERO DE RESULTADOS
function getnumberrows()
{
if (!@$this->qry)
{
$this->selectqry();
}
$this->numberrows=mysql_num_rows($this->qry);
return $this->numberrows;
}
# SETA OS RESULTADOS
function setdbresults($req_results)
{
$this->dbresults = $req_results;
}
# FUNCAO QUE ABRE A CONEXAO COM O BANCO
function openconn()
{
global $TRUE, $FALSE;
$this->connect=mysql_connect("$this->dbhost", "$this->dbuser", "$this->dbpassword")or die("Conexao negada");
$this->dbconnection =mysql_select_db("$this->db")or die("Não existe o banco ".$this->db);
if ($this->dbconnection == $TRUE)
{
$this->setdbconnection($TRUE);
return true;
}
else
{
$this->setdbconnection($FALSE);
return false;
}
}
# RETORNA UM ARRAY DE RESULTADOS -- retorna o resultado com um array associativo
function getdbarray()
{
if (!@$this->qry)
{
$this->selectqry();
}
$this->dbresults = array();
while ($this->row=mysql_fetch_assoc($this->qry))
{
$this->dbresults[]=$this->row;
}
$this->freeresult();
return $this->dbresults;
}
# RETORNA APENAS UMA LINHA DE RESULTADOS
function getdbresult()
{
$this->selectqry();
$this->dbresults=mysql_fetch_assoc($this->qry);
$this->freeresult();
unset ($this->qry);
return $this->dbresults;
}
# RETORNA APENAS UM UNICO RESULTADO
function getdbrecord($rs)
{
$this->selectqry();
$this->db_result=@mysql_result($this->qry,$rs);
unset ($this->qry);
return $this->db_result;
}
# FUNCAO QUE FECHA A CONEXAO COM O BANCO
function closeconn()
{
if ($this->dbconnection = $TRUE)
{
mysql_close($this->connect);
}
}
function freeresult()
{
mysql_free_result($this->qry );
}
# FUNCAO QUE SELECIONA A QUERY A SER EXECUTADA
function selectqry()
{
global $TRUE, $FALSE;
if ($this->dbconnection == $FALSE)
{
$this->openconn();
}
$this->qry = mysql_query($this->sql) or die ("ERRO => ".mysql_error()."<br>NA QUERY => ".$this->sql."<br>NO ARQUIVO => ".@$_SERVER[SCRIPT_NAME]);
if (!$this->qry)
{
return false;
}
else
{
return $this->qry ;
}
}
# EXECUTA UMA QUERY NO BANCO QUE NAO RETORNA RESULTADOS -- INSERT , UPDATE ou DELETE
function othersql()
{
global $TRUE, $FALSE;
if ($this->dbconnection == $FALSE)
{
$this->openconn();
}
$this->qry = mysql_query($this->sql) or die ("ERRO => ".mysql_error()."<br>NA QUERY => ".$this->sql."<br>NO ARQUIVO => ".$_SERVER["SCRIPT_NAME"]);
if (!$this->qry)
{
unset($this->qry);
return false;
}
else
{
unset($this->qry);
return true;
}
}
# RETORNA O NUMERO DE REGISTROS AFETADOS EM UM INSERT, UPDATE OU DELETE
function affected()
{
$this->affected=mysql_affected_rows();
return $this->affected;
}
# RETORNA O ID DO INSERT
function getlastid()
{
if ($this->affected()>0)
{
$this->lastid=mysql_insert_id();
}
else
{
$this->lastid=false;
}
return $this->lastid;
}
# Inicia a transação
function begin()
{
$this->qry=mysql_query("SET AUTOCOMMIT=0");
$this->qry=mysql_query("BEGIN");
unset($this->qry);
return true;
}
# Finaliza uma transação
function commit()
{
$this->qry=mysql_query("COMMIT");
$this->closeconn();
unset($this->qry);
return true;
}
}
?>