PHP で MySQL のクエリテストする為のコード( Bootstrap 仕様 )

$_GET['text'] で入力された SQL が引き渡されます。
※ GET コマンドなので、IE11 以外ならば SQLは アドレスバーで直接入力ができると思います。
※ php-mysql-test.php?text=SQL文

テーブルの表現には Bootstrap を使用しています( 一応スマホではテーブル部分のみ横スクロールします )

QueryString に text が無い場合と text に有効な文字が全く無い場合は  show variables でシステム変数の一覧を表示します
001.<?php
002.// キャッシュを使用しない
003.session_cache_limiter('nocache');
004.session_start();
005. 
006.// UTF-8
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.罫線等のテーブルのレイアウトは、Bootstrap にて適用
020.table {
021.        border: solid 1px #000;
022.        border-collapse: collapse;
023.}
024. 
025.th,td {
026.        border: solid 1px #000;
027.        padding: 5px;
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.// P で挟んだデータの出力
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.                // TH で挟んだデータの出力
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.                // TDで挟んだデータの出力
109.                print_cell_html( "td", ($ncount + 1) );
110. 
111.                for( $i = 0; $i < $nfield; $i++ ) {
112. 
113.                        // TDで挟んだデータの出力
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.// セルの HTML 出力関数
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>



PHP, データベース