Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Sistema: Sistema
- Finalidade: Classe para conexão com o banco de dados (postgresql ou mysql).
- Autor: Wanderson Ferreira Dias
- Observacao:
- Alteracoes:
- Nro Data Autor - Alteracao
- --- -------- -------------------------------------------------------------------
- 001 20.05.06 Wanderson - 1º versão do programa
- */
- class db {
- var $host = "localhost";
- var $banco;
- var $usuario = 'usuariobanco';
- var $senha = 'senhabanco';
- var $r = array();
- var $id_conectar = false;
- var $id_query = false;
- var $erro=false;
- var $sgbd = 'mysql'; // tipo do banco: postgresql ou mysql
- var $posicao = 0;
- function db($banco,$sgbd='') {
- $this->banco = $banco;
- if ($sgbd) $this->sgbd = $sgbd;
- }
- function erro($msg='') {
- if ($msg) {
- $aux = addslashes(trim(str_replace("\n","",$msg)));
- $this->erro = $msg;
- }
- return $this->erro;
- }
- function conectar() {
- if ($this->sgbd=='mysql') {
- $this->id_conectar = mysql_connect($this->host, $this->usuario, $this->senha, true) or die ('erro');
- @mysql_select_db($this->banco);
- } else if ($this->sgbd=='postgresql') {
- if (!$GLOBALS['tty']) $GLOBALS['tty']=1;
- else $GLOBALS['tty']++;
- $aux = 'host='.$this->host.' dbname='.$this->banco;
- if ($this->usuario) $aux.=' user='.$this->usuario;
- if ($this->senha) $aux.=' password='.$this->senha;
- $aux.=' tty='.$GLOBALS['tty'];
- $this->id_conectar = @pg_connect($aux);
- }
- if (!$this->id_conectar) {
- echo 'Erro ao conectar ao banco de dados';
- exit;
- }
- }
- function exec($sql) {
- if (!$this->id_conectar) $this->conectar();
- if ($this->sgbd=='mysql') {
- $this->id_query = @mysql_query($sql);
- $this->erro(@mysql_error($this->id_conectar));
- } else if ($this->sgbd=='postgresql') {
- $this->posicao = 0;
- $this->id_query = @pg_query($this->id_conectar,$sql);
- $this->erro(@pg_last_error($this->id_conectar));
- }
- return $this->erro;
- }
- function fetch($type='A') {
- if ($this->sgbd=='mysql') {
- $result_types = array('A'=>MYSQL_ASSOC,'N'=>MYSQL_NUM,'B'=>MYSQL_BOTH,'a'=>MYSQL_ASSOC,'n'=>MYSQL_NUM,'b'=>MYSQL_BOTH);
- $this->r = @mysql_fetch_array($this->id_query,$result_types[$type]);
- } else if ($this->sgbd=='postgresql') {
- if ($this->posicao>$this->numrows()) {
- $this->r = array();
- return false;
- }
- $result_types = array('A'=>PGSQL_ASSOC,'N'=>PGSQL_NUM,'B'=>PGSQL_BOTH,'a'=>PGSQL_ASSOC,'n'=>PGSQL_NUM,'b'=>PGSQL_BOTH);
- $this->r = @pg_fetch_array($this->id_query,$this->posicao,$result_types[$type]);
- $this->posicao++;
- }
- if ($this->r) return true;
- else return false;
- }
- function numrows() {
- if ($this->sgbd=='mysql') return @mysql_num_rows($this->id_query);
- else if ($this->sgbd=='postgresql') return @pg_num_rows($this->id_query);
- }
- function begin() {
- $this->erro = false;
- return $this->exec("BEGIN");
- }
- function commit() {
- $this->erro = false;
- return $this->exec("COMMIT");
- }
- function rollback() {
- $this->erro = false;
- return $this->exec("ROLLBACK");
- }
- function end() {
- $this->erro = false;
- return $this->exec("ROLLBACK");
- }
- function free_result() {
- if ($this->sgbd=='mysql') return @mysql_free_result($this->id_query);
- else if ($this->sgbd=='postgresql') return @pg_free_result($this->id_query);
- }
- function registros_afetados() {
- if ($this->sgbd=='mysql') return @mysql_affected_rows($this->id_conectar);
- else if ($this->sgbd=='postgresql') return @pg_affected_rows($this->id_query);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment