ただ、select 構文を自由に入力して結果を表示する、シンプルな PHP のサンプルです。 デモページ (デモの為、select * from 社員マスタ しか実行できません) PHP と HTML 部分 : query_data.php
<?php session_cache_limiter('nocache'); session_start(); $dt = date('Y/m/d H:i:s'); if ( $_GET["text"] == "" ) { $_GET["text"] = "select * from syain"; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>MySQL SQL 実行</title> <link rel="stylesheet" href="query_data.css"> </head> <body> <h3> <?= $dt ?> <input class="resetbtn" type="button" value="リセット" onclick='location.href="<?= $_SERVER["PHP_SELF"] ?>"'> </h3> <form method="get" action="query_data.php" > <textarea name="text"><?= $_GET["text"] ?></textarea> <br> <input type="submit" name="send" value="送信" > </form> <?php $mysqli = new mysqli('サーバ', 'ユーザ', 'パスワード', 'データベース'); $mysqli->set_charset("utf8"); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } $sql = $_GET["text"]; if ($result = $mysqli->query($sql)) { print "<table>"; $first = true; while ($row = $result->fetch_assoc()) { if ( $first ) { // タイトル(列名)部分 print "<tr>"; $first = false; foreach ($row as $key => $value) { print "<td>{$key}</td>"; } print "</tr>"; } print "<tr>"; // データ部分 foreach ($row as $key => $value) { print "<td>{$value}</td>"; } print "</tr>"; } print "</table>"; $result->close(); } else { print "<div class='error'>{$mysqli->error}</div>"; } $mysqli->close(); ?> <pre class="debug"> ▼ $_GET の表示 <?php print_r($_GET); ?> </pre> </body> </html>
CSS 部分 : query_data.css
@charset "UTF-8"; body { margin: 0px; } body,h3,textarea,input,pre,td,th,pre { font-family: "ヒラギノ角ゴPro W6","Hiragino Kaku Gothic Pro W6","メイリオ",Meiryo,"MS Pゴシック",Verdana,Arial,Helvetica,sans-serif; } h3 { margin: auto; background-color: cornsilk; padding: 5px 0px 5px 20px; border: 1px solid #c0c0c0; border-radius:0px; box-shadow: 0 15px 10px -10px rgba(0, 0, 0, 0.5), 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset; } form, table, .error, .debug { margin: 30px 0px 20px 30px; } textarea { width: 800px; height: 120px; font-size: 16px; } pre { font-size: 16px; } h3 input { background-color: royalblue; color: white; position: relative; top: -2px; left: 20px; } table { border-collapse: collapse; } table, td { border: solid #a0a0a0 1px; } td { padding: 4px; } .error { color: crimson; } .debug { box-shadow: 0 15px 10px -10px rgba(0, 0, 0, 0.5), 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset; margin-bottom: 200px; margin-right: 100px; padding: 20px; }
|