-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
79 lines (52 loc) · 1.5 KB
/
api.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
<?php
//conexão
$conn = mysqli_connect('localhost', 'root', 'bcd127', 'dbTesteAPI');
// pegar o metodo da chamada, POST ou GET
$method = $_SERVER['REQUEST_METHOD'];
//Via POST faremos o update e o insert
if ($method == "POST"){
$acao = $_POST["acao"];
$tabela = $_POST["tabela"];
$valores = $_POST["valores"];
$sql = "";
if ($acao == "INSERT") {
$sql = "INSERT INTO $tabela set $valores;";
}
if ($acao == "UPDATE") {
$sql = "UPDATE $tabela set $valores;";
}
//Executar ação no banco
$result = mysqli_query($conn, $sql);
echo $result;
}else if ($method == "GET"){
//via get faremos o select e o delete
$acao = $_GET["acao"];
$tabela = $_GET["tabela"];
$where = $_GET["where"];
$sql = "";
if ($acao == "SELECT") {
if($where != ""){
$where = "where $where";
}
$sql = "SELECT * from $tabela $where;";
//Executar ação no banco
$result = mysqli_query($conn, $sql);
$arrayRetorno = array();
if (mysqli_num_rows($result) > 0) {
while($item = mysqli_fetch_assoc($result)) {
$arrayRetorno[] = $item;
}
} else {
echo json_encode($arrayRetorno);
}
}
if ($acao == "DELETE") {
$sql = "DELETE from $tabela $where;";
//Executar ação no banco
$result = mysqli_query($conn, $sql);
echo $result ;
}
}else{
echo json_encode(array("erro" => true , "mensagem" => "metodo não suportado"));
}
?>