Skip to content

Instantly share code, notes, and snippets.

@diegogurgel
Last active December 18, 2015 17:39
Show Gist options
  • Save diegogurgel/5819901 to your computer and use it in GitHub Desktop.
Save diegogurgel/5819901 to your computer and use it in GitHub Desktop.
teste conexão banco
<?php
class Account{
private $id;
private $nomeConta;
private $dataCriacao;
private $dataEdicao;
function getNomeConta()
{
return $this->nomeConta;
}
function setNomeConta($nomeConta)
{
$this->nomeConta = $nomeConta;
return $this;
}
function getDataCriacao()
{
return $this->dataCriacao;
}
function setDataCriacao($dataCriacao)
{
$this->dataCriacao = $dataCriacao;
return $this;
}
function getDataEdicao()
{
return $this->dataEdicao;
}
function setDataEdicao($dataEdicao)
{
$this->dataEdicao = $dataEdicao;
return $this;
}
}
?>
<?php
//require_once("../Class/DataBase/db.php");
require_once("DBInterface.php");
require_once("Account.php");
/**
*
*/
$conta = new account();
$conta->setId(100);
$conta->setNomeConta("uuu");
$conta->setDataCriacao("2013-06-18 00:00:00");
$conta->setDataEdicao("2013-06-18 00:00:00");
$contaDao = new AccountDao();
$contaDao->insert($conta);
$result = $contaDao->getAccount(98);
$contaDao->updateAccount($conta);
$contaDao->removeObj($conta);
class AccountDao
{
function insert($account)
{
echo $account->getNomeConta();
$sql="INSERT INTO accounts ( name , created , modified) VALUES ('".$account->getNomeConta()."','". $account->getDataCriacao()."','". $account->getDataEdicao()."')";
echo $sql;
$interface = new DBInterface();
$result = $interface->executeSQL($sql);
}
function getAccount($id='')
{
$interface = new DBInterface();
if($id!=''){
$sql = "SELECT * FROM accounts WHERE id = $id";
}else{
$sql = "SELECT * FROM accounts";
}
$result = $interface->getObj($sql) ;
return $result;
}
function updateAccount($account)
{
$interface = new DBInterface();
$sql = "UPDATE accounts SET name = '".$account->getNomeConta()."', created = '".$account->getDataCriacao()."' =modified='".$account->getDataEdicao()."' WHERE id =".$account->getId().";";
$result = $interface->executeSQL($sql);
}
function removeObj($account){
//DELETE FROM `trabalhofinance`.`accounts` WHERE `accounts`.`id` = 92;
$interface = new DBInterface();
$sql = "DELETE FROM accounts WHERE id =".$account->getId().";";
$result = $interface->executeSQL($sql);
}
}
?>
<?php
require_once("DBInterface.php");
class AccountDao
{
function insert($budget)
{
$sql="INSERT INTO budgets (created , modified) VALUES ('". $budget->getDataCriacao()."','". $budget->getDataEdicao()."')";
$interface = new DBInterface();
$result = $interface->executeSQL($sql);
}
function getbudget($id='')
{
$interface = new DBInterface();
if($id!=''){
$sql = "SELECT * FROM budgets WHERE id = $id";
}else{
$sql = "SELECT * FROM budgets";
}
$result = $interface->getObj($sql) ;
return $result;
}
function updatebudget($budget)
{
$interface = new DBInterface();
$sql = "UPDATE budgets SET created = '".$budget->getDataCriacao()."' =modified='".$budget->getDataEdicao()."' WHERE id =".$budget->getId().";";
$result = $interface->executeSQL($sql);
}
function removeObj($budget){
$interface = new DBInterface();
$sql = "DELETE FROM budgets WHERE id =".$budget->getId().";";
$result = $interface->executeSQL($sql);
}
}
?>
<?php
class DBInterface{
private $conexao="";
private $database="";
private function connect()
{
$this->conexao = mysql_connect("localhost","root","usbw");
if(!$this->conexao)
{
die ("Falha na coneco com o Banco de Dados!");
}else
{
$this->dataBase = mysql_select_db("trabalhofinance");
}
}
private function close(){
mysql_close($this->conexao);
}
function getObj($sql)
{
$this->connect();
$result = mysql_query($sql, $this->conexao);
$this->close();
return $result;
}
function executeSQL($sql)
{
echo "<br/>$sql <br/>";
$this->connect();
$result = mysql_query($sql, $this->conexao);
echo mysql_affected_rows();
echo mysql_error() ."<br/>";
$this->close();
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment