php - 如何在 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"
?
解决方案
您几乎完成了代码。您只需要在此处添加值:
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();
以上将生成几行按钮。如果那是你需要的。:)
推荐阅读
- java - 将文本字段的值和 SharedPreferences 中保存的值添加到数组
- go - 将 protobuf 对象哈希为字符串作为 redis 数据库的键
- java - 多线程池服务器在 Java 中侦听多个端口 -
- java - Spring JPA 存储库空参数
- docker - 基于 SSH 的 Docker 套接字
- python - Python ctypes OSError:[WinError 1114] 动态链接库 (DLL) 初始化例程失败
- c - :( 将“a”加密为“b”,使用 1 作为密钥预期“密文:b\...”,而不是“明文:a\...”
- javascript - 无法用 JavaScript 中的正则表达式匹配包含不是字母的字符的单词
- subdomain - 使用子域时,图形或 CSS 文件不起作用
- xamarin - 是否可以在 Xamarin Forms 中更改 IOS 本机日期控件的字体大小?