001.
<?php
002.
003.
session_cache_limiter(
'nocache'
);
004.
session_start();
005.
006.
007.
header(
"Content-Type: text/html; charset=utf-8"
);
008.
009.
?>
010.
<!DOCTYPE html>
011.
<html>
012.
<head>
013.
<meta charset=
"utf-8"
>
014.
<meta content=
"width=device-width initial-scale=1.0 minimum-scale=1.0 maximum-scale=1.0 user-scalable=no"
name=
"viewport"
>
015.
<title>SQL実行結果</title>
016.
<link rel=
"stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css"
/>
017.
<style>
018.
019.
020.
021.
022.
023.
024.
025.
026.
027.
028.
029.
030.
</style>
031.
</head>
032.
<body>
033.
<!-- Bootstrap の alert でタイトル -->
034.
<div
class
=
"alert alert-dark"
>
035.
MySQL Query TEST
036.
</div>
037.
038.
<?php
039.
if
( !isset(
$_GET
[
'text'
] ) || trim(
$_GET
[
'text'
]) ==
""
) {
040.
041.
$_GET
[
'text'
] =
"show variables"
;
042.
}
043.
044.
045.
print_cell_html(
"p"
,
$_GET
[
'text'
] );
046.
047.
$server
=
'localhost'
;
048.
$dbname
=
'lightbox'
;
049.
$user
=
'root'
;
050.
$password
=
'パスワード'
;
051.
052.
053.
054.
055.
$mysqli
= @
new
mysqli(
$server
,
$user
,
$password
,
$dbname
);
056.
if
(
$mysqli
->connect_error) {
057.
print
"接続エラーです : ({$mysqli->connect_errno}) ({$mysqli->connect_error})"
;
058.
exit
();
059.
}
060.
061.
062.
063.
064.
$mysqli
->set_charset(
"utf8"
);
065.
066.
067.
068.
069.
$result
=
$mysqli
->query(
$_GET
[
'text'
]);
070.
if
( !
$result
) {
071.
print
"\n"
;
072.
print
"<span style='color:#f00'>error : "
.
$mysqli
->error .
"</span>"
;
073.
exit
();
074.
}
075.
076.
077.
078.
079.
$nfield
=
$result
->field_count;
080.
if
(
$nfield
) {
081.
$ncount
= 0;
082.
print
"<div class='table-responsive-sm'>"
;
083.
print
"<table class='table table-bordered table-hover'><thead class='thead-dark'>\n"
;
084.
085.
086.
print
"\t<th></th>"
;
087.
088.
089.
$field
=
$result
->fetch_fields( );
090.
for
(
$i
= 0;
$i
<
$nfield
;
$i
++ ) {
091.
092.
093.
print_cell_html(
"th"
,
$field
[
$i
]->name );
094.
095.
}
096.
097.
print
"</thead>\n<tbody>\n"
;
098.
099.
100.
101.
102.
103.
while
(
$row
=
$result
->fetch_row()) {
104.
105.
print
"<tr>\n\t"
;
106.
107.
108.
109.
print_cell_html(
"td"
, (
$ncount
+ 1) );
110.
111.
for
(
$i
= 0;
$i
<
$nfield
;
$i
++ ) {
112.
113.
114.
print_cell_html(
"td"
,
$row
[
$i
] );
115.
116.
}
117.
print
"\n</tr>\n"
;
118.
119.
120.
$ncount
++;
121.
}
122.
123.
print
"</tbody></table>"
;
124.
print
"</div>"
;
125.
126.
}
127.
128.
129.
130.
131.
$mysqli
->close();
132.
133.
134.
135.
136.
137.
function
print_cell_html(
$html
,
$data
) {
138.
139.
print <<<CELL_HTML
140.
<{
$html
}>{
$data
}</{
$html
}>
141.
CELL_HTML;
142.
143.
}
144.
145.
?>
146.
147.
</body>
148.
</html>