Ir para conteúdo
Fórum Script Brasil
  • 0

Classe para conexão com o BD


Werez

Pergunta

Olá a todos, estou começando no PHP 5 e resolvi fazer uma DAO para BD, usei MySQL por que era o que eu tinha a mão pra testar ^^ gostaria da opinião de vocês o que mais eu poderia colocar, ou tirar para melhorar o código... abraço a todos...

<?php

/*Nome do Arquivo: Conexão com o Banco de Dados – bancodedados.class.php - v0.1a */

/*Criador: Luís Knob e Paulo Knob */

/*Data de Modificação: 18/09/2008 - 11:35 */

class BancoDeDados{

var $sHost;

var $sLogin;

var $sSenha;

var $sConexao;

var $sCampos;

var $iQntCampos = 0;

var $sTabelas;

var $iTabela;

var $iQntTabelas = 0;

var $sJoin;

var $sIgualdades;

var $bSetJoin = FALSE;

function __construct($sHost, $sLogin, $sSenha){ //Construtor da Classe

$this->sHost = $sHost;

$this->sLogin = $sLogin;

$this->sSenha = $sSenha;

$this->sCampos[0]= "*";

}

function vConectar(){ //Conecta ao MySQL

$this->sConexao = mysql_connect($this->sHost, $this->sLogin, $this->sSenha) or die(mysql_error());

}

function vConexaoBanco($sBanco){ //Escolhe o Banco que Será Usado

$vConexaoBanco = mysql_select_db($sBanco, $this->sConexao) or die(mysql_error());

}

function vDesconectar(){ //Desconecta do MySQL

mysql_close($this->sConexao);

}

function vLimpaValores(){

$this->iQntCampos = 0;

$this->iQntTabelas = 0;

$this->bSetJoin = FALSE;

}

function vSetTabela($sTabelas){//Seta Tabela a Ser Usada

$this->sTabelas[$this->iQntTabelas++] = $sTabelas;

}

function vSetCampo($sCampo, $iTabela = 0){//Seta Campos que Serão Usados

$this->iTabela[$this->iQntCampos] = $iTabela;

$this->sCampos[$this->iQntCampos++] = $sCampo;

}

function vSetJoin($sIgualdades, $sJoin = "INNER"){

$sVetor = explode(", ", $sIgualdades);

$iVetor = count($sVetor);

$sTemp = $this->sTabelas[0];

for($i=0; $i<$iVetor; $i++){

$sTemp .= " ".$sJoin." JOIN ".$this->sTabelas[$i+1]." ON ".$sVetor[$i]." ";

}

$this->sJoin = $sTemp;

$this->bSetJoin = TRUE;

}

function sListar($sWhere = " ", $iTabela = 0){//Lista Valores (SELECT)

if($this->bSetJoin == TRUE){

$sTemp = $this->sTabelas[$this->iTabela[0]].".".$this->sCampos[0];

for($i=1; $i<$this->iQntCampos; $i++)

$sTemp .= ", ".$this->sTabelas[$this->iTabela[$i]].".".$this->sCampos[$i];

$vSql = mysql_query("SELECT ".$sTemp." FROM ".$this->sJoin." WHERE ".$sWhere);

}

else{

$iTemp = 0;

if($this->iQntCampos>0){

for($i=0; $i<$this->iQntCampos; $i++)

if($this->iTabela[$i] == $iTabela)

$sTemp[$iTemp++] = $this->sCampos[$i];

$sCampos = implode(", ", $sTemp);

}

if($sWhere == " ")

$vSql = mysql_query("SELECT ".$sCampos." FROM ".$this->sTabelas[0]) or die(mysql_error());

else

$vSql = mysql_query("SELECT ".$sCampos." FROM ".$this->sTabelas[0]." WHERE ".$sWhere) or die(mysql_error());

}

$i = 1;

while($vLinha = mysql_fetch_array($vSql)){

$vResultado[$i++] = $vLinha;

}

$vResultado[0] = $i;

BancoDeDados::vLimpaValores();

return $vResultado;

}

function vExcluir($sWhere, $iTabela = 0){//Exclui Valores (DELETE)

mysql_query("DELETE FROM ".$this->sTabelas[$iTabela]." WHERE ".$sWhere) or die(mysql_error());

BancoDeDados::vLimpaValores();

}

function vInserir($sValores, $iCampos = 0, $iTabela = 0){//Insere Valores (INSERT)

if($iCampos == 0){

mysql_query("INSERT INTO ".$this->sTabelas[$iTabela]." VALUES (".$sValores.")") or die(mysql_error());

}

else{

$sCampos = implode(", ", $this->sCampos);

mysql_query("INSERT INTO ".$this->sTabelas[$iTabela]." (".$sCampos.") VALUES (".$sValores.")") or die(mysql_error());

}

BancoDeDados::vLimpaValores();

}

function vAlterar($sValores, $sWhere, $iTabela = 0){//Altera Valores (UPDATE)

$sVetor = explode(", ", $sValores);

$sSet = " ";

$sSet .= $this->sCampos[0]." = ".$sVetor[0];

for($i=1; $i<$this->iQntCampos; $i++){

$sSet .= ", ".$this->sCampos[$i]." = ".$sVetor[$i];

}

mysql_query("UPDATE ".$this->sTabelas[$iTabela]." SET ".$sSet." WHERE ".$sWhere) or die(mysql_error());

BancoDeDados::vLimpaValores();

}

}

?>

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...