filipekiss (owner)

Revisions

gist: 86662 Download_button fork
public
Public Clone URL: git://gist.github.com/86662.git
Embed All Files: show embed
class.database.php #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?
 
#####
# Classe database - Controle de Banco de Dados
# Autor: Filipe Kiss
# Release: 1.0.2
# Último release: 2009-03-27
#####
 
Class database
{
var $dbh, $lastResult, $tempCon;
function database($u, $s, $b, $h)
{
$this->__construct($u, $s, $b, $h);
}
 
function __construct($usuario, $senha, $banco, $host)
{
$this->dbh = mysql_connect($host, $usuario, $senha) or die ("Erro Ao Conectar com o Servidor ".$host." com o usuário ".$usuario);
mysql_select_db($banco, $this->dbh) or die ("Servidor Conectado com Sucesso. Erro ao tentar acessar o banco ".$banco." com o usuário ".$usuario);
}
 
function connect()
{
if(empty($this->dbh))
{
mysql_connect($host, $usuario, $senha) or die ("Erro Ao Conectar com o Servidor ".$host." com o usuário ".$usuario);
mysql_select_db($banco) or die ("Servidor Conectado com Sucesso. Erro ao tentar acessar o banco ".$banco." com o usuário ".$usuario);
$this->tempCon = true;
}
}
 
function disconnect()
{
if($this->tempCon == true)
{
mysql_close();
$this->tempCon = false;
}
}
 
function query($query)
{
$this->connect();
$return_val = 0;
$this->result = mysql_query($query, $this->dbh) or die(
"<h3>An Error Ocurred</h3>
<div>".mysql_error()."</div><br />".$query
);
if(mysql_error($this->dbh) != '')
$this->result = false;
$this->lastResult = $this->result;
$this->disconnect();
return $this->result;
}
 
function get_var($query)
{
$this->result = $this->query($query);
if($this->result)
{
if(mysql_num_rows($this->result) != 0)
$this->return_value = mysql_result($this->result, 0);
else
$this->return_value = 0;
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
 
function get_results($query)
{
$this->result = $this->query($query);
if($this->result)
{
$this->return_value = array();
if(mysql_num_rows($this->result) != 0)
{
while($this->data = mysql_fetch_assoc($this->result))
{
$this->return_value[] = $this->data;
}
}
else
{
$this->return_value = 0;
}
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
function get_row($query)
{
$this->result = $this->query($query);
if($this->result)
{
if(mysql_num_rows($this->result) != 0)
{
while($this->data = mysql_fetch_assoc($this->result))
{
if(!$this->tempData)
{
$this->tempData = $this->data;
}
}
$this->return_value = $this->tempData;
unset($this->tempData);
}
else
{
$this->return_value = 0;
}
}
else
{
$this->return_value = false;
}
$this->lastResult = $this->return_value;
return $this->return_value;
}
}
$db_server = 'localhost'; //Geralmente é localhost. caso não funcione, consulte seu provedor
$db_name = 'banco'; //Nome do Banco de Dados
$db_user = 'user'; //Nome do Usuário
$db_passwd = 'senha'; //Senha
 
$mdb = new database($db_user, $db_passwd, $db_name, $db_server);
?>