-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTableList.php
44 lines (40 loc) · 1.33 KB
/
getTableList.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
<?php declare(strict_types = 1);
header('Content-Type:application/json');
$aFilterOpts = [
'DbHost' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_VALIDATE_IP
],
'HostUser' => FILTER_SANITIZE_STRING,
'HostPass' => FILTER_SANITIZE_STRING,
];
$aInput = filter_input_array(INPUT_POST, $aFilterOpts);
$oOpt = (object)$aInput;
try{
$oConn = new PDO('mysql:host=' . $oOpt->DbHost . ';dbname=information_schema', $oOpt->HostUser, (!empty($oOpt->HostPass) ? $oOpt->HostPass : ''));
} catch (Exception $e) {
echo json_encode(['Error' => 'Could not connect to database']);
}
if ($oConn) {
// Fetch table info from the info schema
$aTables = [];
$oStm = $oConn->prepare("
SELECT CONCAT(table_schema, '.', table_name) `Table`
FROM information_schema.tables
WHERE table_schema" .
(!empty($oOpt->DbName)
? " IN (?)"
: " NOT IN ('information_schema','performance_schema','mysql','util_replication')"
)
);
if (!empty($oOpt->DbName)) {
$oStm->execute([implode("','", $oOpt->DbName)]);
} else {
$oStm->execute();
}
foreach ($oStm->fetchAll(PDO::FETCH_OBJ) as $oSchemata) {
$aTables[] = $oSchemata->Table;
}
$aTables = array_unique($aTables);
echo json_encode(array_values($aTables));
}