-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconexao.php
More file actions
52 lines (47 loc) · 1.61 KB
/
Copy pathconexao.php
File metadata and controls
52 lines (47 loc) · 1.61 KB
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
<?php
define('DB_SERVER', 'localhost');
define('DB_NAME', 'cadastro');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
//inicio da classe de conexao
class Conexao {
var $mysqli;
public function __construct($server, $database, $username, $password, $port){
$this->mysqli = new mysqli($server, $username, $password, $database, $port);
}
public function insert($tabela, $dados) {
foreach ($dados as $key => $value) {
$keys[] = $key;
$insertvalues[] = '\'' . $value . '\'';
}
$keys = implode(',', $keys);
$insertvalues = implode(',', $insertvalues);
$sql = "INSERT INTO $tabela ($keys) VALUES ($insertvalues)";
return $this->executar($sql);
}
public function update($tabela, $colunaPrimary, $id, $dados) {
foreach ($dados as $key => $value) {
$sets[] = $key . '=\'' . $value . '\'';
}
$sets = implode(',', $sets);
$sql = "UPDATE $tabela SET $sets WHERE $colunaPrimary = '$id'";
$result = $this->executar($sql);
return $result;
}
public function select($tabela, $colunas = "*") {
$sql = "SELECT $colunas FROM $tabela";
$result = $this->executar($sql);
return $result;
}
public function delete($colunaPrimary, $id, $tabela) {
$sql = "DELETE FROM $tabela WHERE $colunaPrimary = $id";
echo $sql;
$result = $this->executar($sql);
return $result;
}
function executar($query) {
$result = $this->mysqli->query($query);
return $result;
}
}
?>