0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQL実行ツールをつくる

Posted at

タイトルの通りSQL実行ツールを作っていきたいと思います。

まずはHtmlから

<body cz-shortcut-listen="true">
  <form action="index.php" method="post">
    <label>
      SQL:
      <br>
      <textarea name="sql" rows="8" cols="80" style="margin: 0px; width: 560px; height: 92px;"></textarea>
    </label>
    <br>
    <input type="submit" name="exec" value="実行">
  </form>
  <table border="1" style="border-collapse: collapse">

次にPHP

  if (!empty($_POST["sql"])) { //$_POST["sql"]が空じゃない時
    $sql = $_POST["sql"]; //上のtextareaで入力されたもの
  
  $link = mysqli_connect('ここはそれぞれの引数を入れる');//mysqliに接続

  $result =  mysqli_query($link, $sql);
  if (is_bool($result)) { // is_boolは変数がブーリアン(true,false)であるかどうか調べる
    if ($result) {
      echo "成功しました";
    } else {
      echo "失敗しました";
    }
  } else {
    $first = true; // 初回フラグをtrueにしておく
    foreach($result as $row) {
      if ($first) {
        echo "<tr>";
        foreach($row as $key => $value) {
            echo "<th>".$key."</th>";// Table_in_?やフィールドの構成を表示する
        }
        echo "</tr>";
        $first = false; // $keyを表示したのでfalseにしておく
      }
      echo "<tr>";
      foreach((array)$row as $key => $value) {
        echo "<td>".$value."</td>"; //テーブル名やカラムを表示する
      }
      echo "</tr>";
    }
  }
  mysqli_close($link);
  }
  ?>
  </table>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?