-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
95 lines (74 loc) · 1.58 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
$servername = "localhost";
$username = "root";
$password = "toor";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$data = json_decode(file_get_contents("php://input"), true);
if(!isset($data)) {
$data[] = array("action" => $_GET["sql"], "sql" => $_GET["sql"]);
}
$action = $data["action"];
switch ($action) {
case 'test':
$data["ack"] = "true";
echo json_encode($data);
break;
case 'sql':
$sql = $data["sql"];
$result = mysqli_query($conn, $sql);
$names = [];
echo "
<thead>
<tr>";
while ($finfo = mysqli_fetch_field($result)) {
$field = $finfo->name;
echo "
<th> $field </th>";
$names[] = $field;
}
echo "
</tr>
</thead>";
echo "
<tbody>";
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "
<tr>";
foreach ($names as $name) {
echo "
<td>" . $row[$name] . "</td>";
}
echo "
</tr>";
}
}
echo "
</tbody>";
mysqli_free_result($result);
$data["ack"] = "true";
break;
case "test_result":
$sql = $data["sql"];
$myArray = array();
if ($result = $conn->query($sql)) {
$data["ack"] = "true";
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
$result->close();
$conn->close();
echo json_encode($data);
break;
default:
# code...
break;
}
?>