Skip to content

Instantly share code, notes, and snippets.

@icarofreire
Last active August 29, 2015 14:15
Show Gist options
  • Save icarofreire/2a4365fbbf4fc3acf8e2 to your computer and use it in GitHub Desktop.
Save icarofreire/2a4365fbbf4fc3acf8e2 to your computer and use it in GitHub Desktop.
CRUD ADODB.
<?php
/*
* Escrito por: Ícaro Freire Martins
* Data: 20/02/2015
* icarofre99@gmail.com
* http://icarofreire.blogspot.com.br/
* */
include("adodb5/adodb.inc.php");
include("adodb5/toexport.inc.php");
class db_crud {
private $conexao;
function __construct($servidor, $banco, $usuario, $senha){
$this->conexao = ADONewConnection("postgres");
$this->conexao->debug = true;
$this->conexao->PConnect($servidor, $usuario, $senha, $banco);
$this->conexao->SetFetchMode(ADODB_FETCH_ASSOC);
}// fim __construct;
/* Preenchimento de dados da tabela com valores aleatorios para exemplificar informações.
* */
public function preencher_tabela(){
for($i=0; $i<100000; $i++){ // 100000
$id = ($i+1);
$this->inserir(
array($id, rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000))
);
}
echo "Inserido com sucesso.<BR>";
}
public function atualizar_por_id($ID){
$this->atualizar(array(rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000),rand(1000,10000), $ID));
}
public function inserir($data){
$sql = "INSERT INTO associado (id,nome,data_nascimento,telefone_residencial,telefone_celular,email,cpf) VALUES (?,?,?,?,?,?,?)";
if ($this->conexao->Execute($sql,$data) === false){
print "Erro inserir.";
}
}
/* ex: array( $a, ... $z, $ID);
* */
public function atualizar($data) {
$sql = "UPDATE associado SET nome = ?, data_nascimento = ?, telefone_residencial = ?, telefone_celular = ?, email = ?, cpf = ? where id = ? ";
if ($this->conexao->Execute($sql,$data) === false){
print "Erro atualizar.";
}
}
public function deletar($id) {
$sql = "DELETE from associado where id = ? ";
if ($this->conexao->Execute($sql,$id) === false){
print "Erro deletar.";
}
}
/* Exportar informações da tabela para um arquivo .CSV;
* */
public function exportar_csv($nome_do_arquivo){
$sql = "SELECT * FROM associado";
$rs = $this->conexao->Execute($sql);
$rs->MoveFirst();
$fp = fopen($nome_do_arquivo.".csv", "w");
if ($fp){
rs2csvfile($rs, $fp);
fclose($fp);
}
}
}// fim classe crud.
/*
SQL tabela postgres;
CREATE TABLE associado
(
id integer NOT NULL,
nome character varying(200) DEFAULT NULL::character varying,
data_nascimento character varying(200) DEFAULT NULL::character varying,
telefone_residencial character varying(200) DEFAULT NULL::character varying,
telefone_celular character varying(200) DEFAULT NULL::character varying,
email character varying(200) DEFAULT NULL::character varying,
cpf character varying(200) DEFAULT NULL::character varying,
CONSTRAINT associado_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE associado
OWNER TO icaro;
*/
?>
<?php
/*
* Escrito por: Ícaro Freire Martins
* Data: 20/02/2015
* icarofre99@gmail.com
* http://icarofreire.blogspot.com.br/
* */
include("CRUD.php");
$cru = new db_crud("localhost", "postgres", "icaro", "icaro123");
$cru->preencher_tabela(); // preenche a tabela com valores aleatorios;
// $cru->inserir(array( ... )); // inserir
// $cru->atualizar(array( $a, ... $z, $ID)); // atualizar
// $cru->deletar( ID ); // deletar por id
// $cru->exportar_csv("dados"); // exportar em formato CSV
/*
SQL tabela postgres utilizada;
CREATE TABLE associado
(
id integer NOT NULL,
nome character varying(200) DEFAULT NULL::character varying,
data_nascimento character varying(200) DEFAULT NULL::character varying,
telefone_residencial character varying(200) DEFAULT NULL::character varying,
telefone_celular character varying(200) DEFAULT NULL::character varying,
email character varying(200) DEFAULT NULL::character varying,
cpf character varying(200) DEFAULT NULL::character varying,
CONSTRAINT associado_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE associado
OWNER TO icaro;
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment