-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
68 lines (52 loc) · 2.07 KB
/
Copy pathdatabase.php
File metadata and controls
68 lines (52 loc) · 2.07 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
//executa Querys
function DBExecute($query, $insertId=false){
$link=DBConnect();
$result=@mysqli_query($link, $query)or die(mysqli_error($link));
if($insertId)
$result=mysqli_insert_id($link); //se for passado o parametro insertId eu quero q seja retornado o id deste novo
DBClose($link);
return $result;
}
function DBCreate($table, array $data, $insertId=false){
$table=DB_PREFIX.'_'.$table;
$data=DBEscape($data);
$fields=implode(', ', array_keys($data));
$values="'".implode("', '", $data)."'";
$query="INSERT INTO {$table} ( {$fields} ) VALUES ( {$values} )";
return DBExecute($query, $insertId); //insertId para quando deseja q seja retornado o id do novo criado
}
//Ler registros
function DBRead($table, $params=null, $fields='*'){
$table=DB_PREFIX.'_'.$table;
$params=($params) ? " {$params}" : null;
$query="SELECT {$fields} FROM {$table}{$params}";
$result=DBExecute($query);
if(!mysqli_num_rows($result)) //mysqli_num_rows conta o numero de linhas
return false;
else{
while($res=mysqli_fetch_assoc($result)){ //mysqli_fetch_assoc transforma os campos da tabela em array onde os indices são os nomes dos campos
$data[]=$res;
}
return $data;
}
}
//Altera Registros
function DBUpdate($table, array $data, $where=null, $insertId=false){
foreach($data as $key=>$value){
$fields[]="{$key}='{$value}'";
}
$fields=implode(', ', $fields);
$table=DB_PREFIX.'_'.$table;
$where=($where) ? " {$where}" : null;
$query="UPDATE {$table} SET {$fields}{$where}";
return DBExecute($query, $insertId);
}
//Deleta Registros
function DBDelete($table, $where=null){
$table=DB_PREFIX.'_'.$table;
$where=($where) ? " {$where}" : null;
$query="DELETE FROM {$table}{$where}";
return DBExecute($query, true);
}
?>