Skip to content

Instantly share code, notes, and snippets.

@leite
Created February 26, 2013 15:28
Show Gist options
  • Save leite/5039296 to your computer and use it in GitHub Desktop.
Save leite/5039296 to your computer and use it in GitHub Desktop.
example php with db connection
<?php
class usuario{
//variaveis privadas
private $_codCliente;
private $_nome;
private $_email;
private $_endereco;
private $_numero;
private $_complemento;
private $_bairro;
private $_cep;
private $_cidade;
private $_estado;
private $_tel;
//database
private $_db;
//metodos publicos GET/SET
public function setCodCliente( $value ){ $this->_codCliente = $value; }
public function getCodCliente(){ return $this->_codCliente; }
public function setNome( $value ){ $this->_nome = $value; }
public function getNome(){ return $this->_nome; }
public function setEmail( $value ){ $this->_email = $value; }
public function getEmail(){ return $this->_email; }
public function setEndereco( $value ){ $this->_endereco = $value; }
public function getEndereco(){ return $this->_endereco; }
public function setNumero( $value ){ $this->_numero = $value; }
public function getNumero(){ return $this->_numero; }
public function setComplemento( $value ){ $this->_complemento = $value; }
public function getComplemento(){ return $this->_complemento; }
public function setBairro( $value ){ $this->_bairro = $value; }
public function getBairro(){ return $this->_bairro; }
public function setCEP( $value ){ $this->_cep = $value; }
public function getCEP(){ return $this->_cep; }
public function setCidade( $value ){ $this->_cidade = $value; }
public function getCidade(){ return $this->_cidade; }
public function setEstado( $value ){ $this->_estado = $value; }
public function getEstado(){ return $this->_estado; }
public function setTel( $value ){ $this->_tel = $value; }
public function getTel(){ return $this->_tel; }
//contrutor
public function __construct(){
//conecta no banco
$this->_db = mysql_connect('localhost', 'root', 'novell');
//usa base de dados espaco
mysql_select_db( 'espaco', $this->_db );
}
//destrutor
public function __destruct(){ mysql_close( $this->_db ); }
public function pegaIsso(){
return $this->_isso;
}
//cadastra usuario
public function cadastra(){
try{
//checa duplicidade
if( $this->duplicidade() ){ return 'Nome ou e-mail já cadastrado, por favor informe novamente'; }
//cadastra
$res = mysql_query( "INSERT INTO clientes (" .
" nome, email, endereco, numero, " .
" complemento, bairro, cep, cidade, " .
" estado, tel, dtCadastro ) VALUES ( " .
" '". $this->_nome ."'," .
" '". $this->_email ."'," .
" '". $this->_endereco ."'," .
" '". $this->_numero ."'," .
" '". $this->_complemento ."'," .
" '". $this->_bairro ."'," .
" '". $this->_cep ."'," .
" '". $this->_cidade ."'," .
" '". $this->_estado ."'," .
" '". $this->_tel ."'," .
" NOW() )", $this->_db );
//checa por erros no cadastro
if( !$res ){ debugit(array('PASS'=>'USER THROW ERROR 4')); throw new Exception('Erro ao efetuar cadastro'); }
//pega o id do cara
$res = mysql_query( "SELECT codCliente FROM clientes WHERE nome='" .
$this->_nome ."' AND email='". $this->_email ."'", $this->_db );
//
if( !$res ){
debugit(array('PASS'=>'USER THROW ERROR 5'));
throw new Exception('Erro ao efetuar cadastro');
}else{
if( mysql_num_rows($res)>0 ){
//return true;
$row= mysql_fetch_array( $res );
$this->_codCliente = $row['codCliente'];
return true;
}else{
debugit(array('PASS'=>'USER THROW ERROR 6'));
throw new Exception('Erro ao efetuar cadastro');
}
}
}catch( Exception $e ){
//return 'Erro ao efetuar cadastro';
return false;
}
}
//duplicidade
public function duplicidade(){
$sql = "SELECT * FROM clientes WHERE nome='". $this->_nome ."' AND email='". $this->_email ."'";
$res = mysql_query( $sql, $this->_db );
debugit(array('PASS'=>'USER SQL: ' .$sql ));
if( $res ){
if( mysql_num_rows($res)>0 ){
$row = mysql_fetch_array( $res );
$this->_codCliente = $row['codCliente'];
$this->_nome = $row['nome'];
$this->_email = $row['email'];
$this->_endereco = $row['endereco'];
$this->_numero = $row['numero'];
$this->_complemento = $row['complemento'];
$this->_bairro = $row['bairro'];
$this->_cep = $row['cep'];
$this->_cidade = $row['cidade'];
$this->_estado = $row['estado'];
$this->_tel = $row['tel'];
return true;
}
}
return false;
}
//localiza usuario
public function localiza(){
try{
$res;
if( $this->_codCliente>0 ){
$res = mysql_query( "SELECT * FROM clientes WHERE codCliente=". $this->_codCliente, $this->_db );
}else{
$res = mysql_query( "SELECT * FROM clientes WHERE nome='". $this->_nome ."' AND email='". $this->_email ."'", $this->_db );
}
if( !$res ){
throw new Exception('Erro ao localizar');
}else{
if( mysql_num_rows($res)>0 ){
$row = mysql_fetch_array( $res );
$this->_codCliente = $row['codCliente'];
$this->_nome = $row['nome'];
$this->_email = $row['email'];
$this->_endereco = $row['endereco'];
$this->_numero = $row['numero'];
$this->_complemento = $row['complemento'];
$this->_bairro = $row['bairro'];
$this->_cep = $row['cep'];
$this->_cidade = $row['cidade'];
$this->_estado = $row['estado'];
$this->_tel = $row['tel'];
return true;
}
}
return false;
}catch( Exception $e ){
return false;
}
}
//lista usuarios
public function lista( $__status=-1 ){
try{
$res;
if( $_status==-1 ){
$res = mysql_query( "SELECT * FROM clientes", $this->_db );
}else{
$res = mysql_query( "SELECT * FROM clientes LEFT JOIN transacao ON (transacao.codCliente=clientes.codCliente) WHERE transacao.status=" . $__status, $this->_db );
}
if( !$res ){
throw new Exception('Erro ao listar');
}else{
if( mysql_num_rows($res)>0 ){
return $res;
}else{
return array();
}
}
return false;
}catch( Exception $e ){
return false;
}
}
function debugit($array){
//DEBUG!
$fp=fopen("test.txt","a");
foreach($array as $key => $value){
fwrite($fp, $key." => ".$value."\r\n");
}
fclose($fp);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment