-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
136 lines (132 loc) · 4.73 KB
/
index.php
File metadata and controls
136 lines (132 loc) · 4.73 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
require 'vendor/autoload.php';
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);
// Using Medoo namespace
use Medoo\Medoo;
$config = [
'db' => trim($_GET['name'] ?: ''),
'host' => trim($_GET['host'] ?: '127.0.0.1'),
'user' => trim($_GET['user'] ?: 'root'),
'pwd' => trim($_GET['pwd'] ?: ''),
];
$flag = false;
if ($config['db']) {
setcookie('config', json_encode($config));
$flag = true;
$db = new Medoo([
'database_type' => 'mysql',
'charset' => 'utf8mb4',
'database_name' => $config['db'],
'server' => $config['host'],
'username' => $config['user'],
'password' => $config['pwd'],
]);
$rs = $db->query('show tables')->fetchAll();
foreach ($rs as $v) {
$tb = $v[0];
$tbs[] = $tb;
/**
* view table comments
* @var string
*/
$sql = "select *
from information_schema.tables
where table_schema = '" . $config['db'] . "'
and table_name = '" . $tb . "'";
$fet = $db->query($sql)->fetch();
$table_comm = $fet['TABLE_COMMENT'];
$sql = "SHOW FULL COLUMNS FROM " . $tb;
$cols = $db->query($sql)->fetchAll();
unset($list);
foreach ($cols as $v1) {
$list[] = [
$v1['Field'],
$v1['Comment'],
];
}
$arr[$tb] = [
$table_comm,
$list,
];
}
$str = '';
$num = 1;
foreach ($arr as $tab => $li) {
$str .= "## $num.表名 " . $tab . "[" . $li[0] . "]\n";
$str .= "|字段名|备注|\n| :-: | :-: |\n";
foreach ($li[1] as $v) {
$str .= "|" . $v[0] . "|" . $v[1] . "|\n";
}
$num++;
}
$str_md = '';
$num = 1;
foreach ($arr as $tab => $li) {
$str_md .= '\n## ' . $num . '.表名 ' . $tab . '[' . $li[0] . ']\n';
$str_md .= '|字段名|备注|\n| :-: | :-: |\n';
foreach ($li[1] as $v) {
$str_md .= '|' . $v[0] . '|' . $v[1] . '|\n';
}
$num++;
}
}
$cookie = $_COOKIE['config'];
if ($cookie) {
$cookie = json_decode($cookie, true);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootswatch: Default</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<link rel="stylesheet" href="https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css" media="screen">
</head>
<body>
<div class="container" style="margin-top: 100px;">
<div class="page-header" id="banner" style="margin-bottom: 60px;">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<?php if ($flag == true) { ?>
<h3>复制以下内容,放到markdown编辑器中使用。 </h3>
<?php } else { ?>
<h3>MYSQL表结构生成Markdown格式 </h3>
<?php } ?>
</div>
</div>
</div>
<?php if ($flag == true) { ?>
<div class="alert alert-dismissible alert-info">
<textarea style="width: 100%;height: 500px;"><?php echo $str; ?></textarea>
</div>
<div id="content"></div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
document.getElementById('content').innerHTML = marked('<?php echo $str_md; ?>');
</script>
<?php } else { ?>
<form method="get">
<div class="form-group">
<label class="col-form-label" for="inputDefault">数据库主机地址</label>
<input type="text" class="form-control" value="<?php echo trim($_GET['host'] ?: $cookie['host']); ?>" placeholder="" name="host">
</div>
<div class="form-group">
<label class="col-form-label" for="inputDefault">用户名</label>
<input type="text" class="form-control" value="<?php echo trim($_GET['user'] ?: $cookie['user']); ?>" placeholder="" name="user">
</div>
<div class="form-group">
<label class="col-form-label" for="inputDefault">密码</label>
<input type="text" class="form-control" value="<?php echo trim($_GET['pwd'] ?: $cookie['pwd']); ?>" placeholder="" name="pwd">
</div>
<div class="form-group">
<label class="col-form-label" for="inputDefault">数据库名</label>
<input type="text" class="form-control" placeholder="" value="<?php echo trim($_GET['db'] ?: $cookie['db']); ?>" name="name">
</div>
<button type="submit" class="btn btn-primary">生成</button>
</form>
<?php } ?>
</div>
</body>
</html>