首页 > 解决方案 > 如何在 HTML 按钮中从 mySql 加载数据?

问题描述

我想从我的 MySql 数据库中加载数据并将该数据库的一个字段放入按钮的文本中:

<?php
  $servername = "localhost";
  $username   = "prova";
  $password   = "";
  $dbname     = "prova";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }

  $sql    = "SELECT nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
      // output data of each row
      while ($row = $result->fetch_assoc()) {
          echo "nomeCantiere: " . $row["nomeCantiere"] . " - codieCommessa: " . $row["codieCommessa"] . " - indirizzoCantiere" . $row["indirizzoCantiere"] . "<br>";
      }
  } else {
      echo "0 results";
  }
  $conn->close();
?>
<input type="button" value="button" />

我是这种编程的初学者。

现在我设法做的是生成一个简单的"echo".

如何自动生成带有 的按钮value="nomeCantiere"

标签: phphtmlmysqlweb-applications

解决方案


您几乎完成了代码。您只需要在此处添加值:

if ($result->num_rows > 0) {
  // output data of each row
  while ($row = $result->fetch_assoc()) {
    //// Look at this line ////
    echo '<input type="button" value="' . $row["nomeCantiere"] . '" />';
  }
} else {
  echo "0 results";
}
$conn->close();

以上将生成几行按钮。如果那是你需要的。:)


推荐阅读