-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_exec.php
92 lines (73 loc) · 2.83 KB
/
query_exec.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
<?php
// json response array
$response = array();
$error = false;
if(isset($_POST["prep_query"]) && isset($_POST["param_type"]) && isset($_POST["params"]))
{
require_once "DB_Functions.php";
$db = new DB_Functions();
$prep_querys = json_decode($_POST["prep_query"]);
$param_types = json_decode($_POST["param_type"]);
$params = json_decode($_POST["params"]);
$transaction = isset($_POST["transaction"]) ? json_decode($_POST["transaction"])[0] : false;
if ($transaction === true)
$db->conn->begin_transaction();
for ($i = 0; $i < count($prep_querys); $i++)
{
$err = true;
// Prepare statement
$stmt = $db->conn->prepare($prep_querys[$i]);
if ($stmt) {
$paramsi = $params[$i];
$bind = true;
if(count($paramsi))
$bind = $stmt->bind_param($param_types[$i], ...$paramsi);
// Try to execute prepered statement
if ($bind && $stmt->execute())
{
$hasResult = $stmt->get_result();
if ($hasResult)
{
$result = $hasResult->fetch_all(MYSQLI_ASSOC);
}
else
{
$result = true;
$response["queries"][$i]["id"] = $db->conn->insert_id;
}
$stmt->close();
unset($stmt);
$err = false;
$response["queries"][$i]["error"] = false;
$response["queries"][$i]["result"] = $result;
}
else
{
$response["queries"][$i]["error"] = true;
$response["queries"][$i]["error_msg"] = "Si è verificato un errore imprevisto: " . $stmt->error;;
$response["error_msg"] = $response["queries"][$i]["error_msg"];
}
} else {
$response["queries"][$i]["error"] = true;
$response["queries"][$i]["error_msg"] = "La query non è andata a buon fine: " . mysqli_error($db->conn);
$response["error_msg"] = $response["queries"][$i]["error_msg"];
}
$error = $error || $err;
}
if ($transaction === true) {
if ($error)
$db->conn->rollback();
else
$db->conn->commit();
}
$response["error"] = $error;
} else {
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori";
}
$db->conn->close();
unset($db->conn);
unset($db);
$stampa = json_encode($response);
echo $stampa;
?>