Guest User

Untitled

a guest
Aug 2nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <?php
  2. /*
  3. Sistema: Sistema
  4. Finalidade: Classe para conexão com o banco de dados (postgresql ou mysql).
  5. Autor: Wanderson Ferreira Dias
  6. Observacao:
  7. Alteracoes:
  8. Nro Data Autor - Alteracao
  9. --- -------- -------------------------------------------------------------------
  10. 001 20.05.06 Wanderson - 1º versão do programa
  11. */
  12. class db {
  13. var $host = "localhost";
  14. var $banco;
  15. var $usuario = 'usuariobanco';
  16. var $senha = 'senhabanco';
  17. var $r = array();
  18. var $id_conectar = false;
  19. var $id_query = false;
  20. var $erro=false;
  21. var $sgbd = 'mysql'; // tipo do banco: postgresql ou mysql
  22. var $posicao = 0;
  23. function db($banco,$sgbd='') {
  24. $this->banco = $banco;
  25. if ($sgbd) $this->sgbd = $sgbd;
  26. }
  27. function erro($msg='') {
  28. if ($msg) {
  29. $aux = addslashes(trim(str_replace("\n","",$msg)));
  30. $this->erro = $msg;
  31. }
  32. return $this->erro;
  33. }
  34. function conectar() {
  35. if ($this->sgbd=='mysql') {
  36. $this->id_conectar = mysql_connect($this->host, $this->usuario, $this->senha, true) or die ('erro');
  37. @mysql_select_db($this->banco);
  38. } else if ($this->sgbd=='postgresql') {
  39. if (!$GLOBALS['tty']) $GLOBALS['tty']=1;
  40. else $GLOBALS['tty']++;
  41. $aux = 'host='.$this->host.' dbname='.$this->banco;
  42. if ($this->usuario) $aux.=' user='.$this->usuario;
  43. if ($this->senha) $aux.=' password='.$this->senha;
  44. $aux.=' tty='.$GLOBALS['tty'];
  45. $this->id_conectar = @pg_connect($aux);
  46. }
  47. if (!$this->id_conectar) {
  48. echo 'Erro ao conectar ao banco de dados';
  49. exit;
  50. }
  51. }
  52. function exec($sql) {
  53. if (!$this->id_conectar) $this->conectar();
  54. if ($this->sgbd=='mysql') {
  55. $this->id_query = @mysql_query($sql);
  56. $this->erro(@mysql_error($this->id_conectar));
  57. } else if ($this->sgbd=='postgresql') {
  58. $this->posicao = 0;
  59. $this->id_query = @pg_query($this->id_conectar,$sql);
  60. $this->erro(@pg_last_error($this->id_conectar));
  61. }
  62. return $this->erro;
  63. }
  64. function fetch($type='A') {
  65. if ($this->sgbd=='mysql') {
  66. $result_types = array('A'=>MYSQL_ASSOC,'N'=>MYSQL_NUM,'B'=>MYSQL_BOTH,'a'=>MYSQL_ASSOC,'n'=>MYSQL_NUM,'b'=>MYSQL_BOTH);
  67. $this->r = @mysql_fetch_array($this->id_query,$result_types[$type]);
  68. } else if ($this->sgbd=='postgresql') {
  69. if ($this->posicao>$this->numrows()) {
  70. $this->r = array();
  71. return false;
  72. }
  73. $result_types = array('A'=>PGSQL_ASSOC,'N'=>PGSQL_NUM,'B'=>PGSQL_BOTH,'a'=>PGSQL_ASSOC,'n'=>PGSQL_NUM,'b'=>PGSQL_BOTH);
  74. $this->r = @pg_fetch_array($this->id_query,$this->posicao,$result_types[$type]);
  75. $this->posicao++;
  76. }
  77. if ($this->r) return true;
  78. else return false;
  79. }
  80. function numrows() {
  81. if ($this->sgbd=='mysql') return @mysql_num_rows($this->id_query);
  82. else if ($this->sgbd=='postgresql') return @pg_num_rows($this->id_query);
  83. }
  84. function begin() {
  85. $this->erro = false;
  86. return $this->exec("BEGIN");
  87. }
  88. function commit() {
  89. $this->erro = false;
  90. return $this->exec("COMMIT");
  91. }
  92. function rollback() {
  93. $this->erro = false;
  94. return $this->exec("ROLLBACK");
  95. }
  96. function end() {
  97. $this->erro = false;
  98. return $this->exec("ROLLBACK");
  99. }
  100. function free_result() {
  101. if ($this->sgbd=='mysql') return @mysql_free_result($this->id_query);
  102. else if ($this->sgbd=='postgresql') return @pg_free_result($this->id_query);
  103. }
  104. function registros_afetados() {
  105. if ($this->sgbd=='mysql') return @mysql_affected_rows($this->id_conectar);
  106. else if ($this->sgbd=='postgresql') return @pg_affected_rows($this->id_query);
  107. }
  108. }
  109. ?>
Add Comment
Please, Sign In to add comment