我想从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();
上面将生成几行按钮。如果你需要的话。
:)
关于php - 如何在HTML按钮中从mySql加载数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52552326/