我有一个包含数千行代码的表,我必须在表的一些"td“中加载来自数据库的一些数据……我发现了几个上传的例子,但它们都需要重写整个表……所以我问您,有没有一种更简单的方法来在表中传递数据库的值?
简单地说,使用SELECT,我必须能够在"td“中加载包含textBox的数据。不需要重写表格(假设有数千行)……
我附上了我在SO上找到的例子:
1°:Load data from MySQL database to HTML textboxes on button click
发布于 2018-10-02 17:56:44
您想用数据库数据填充<table>吗?
1)构建sql查询。
2)执行和检索数据。
3)使用foreach向<table>注入数据。
<?php
//Connection to db
$db = new mysqli('127.0.0.1', 'login', 'pass', 'database');
//Query
$sql = "SELECT name, surname FROM example";
//Execution + error checking
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()){
//Displaying results in table rows
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["surname"]."</td>
</tr>";
}
?>
</tbody>
</table>发布于 2018-10-02 18:01:42
要将MySQL/MariaDB数据库中的数据加载到表中,可以使用以下代码:
<!DOCTYPE html>
<?php
//This is the connection
$mysqli = new mysqli('localhost', 'root', 'DatabasePassword', 'Database Name'); ?>
<html>
<head>
<title></title>
</head>
<body>
<div class="content">
<table class='table table-bordered table-striped'>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php while ($row = $mysqli->fetch_array(MYSQLI_ASSOC)): ?>
<?php foreach ($row as $key => $value): ?>
<td alt="<?=$key?>"><?=$value?></td>
<?php endforeach; ?>
<?php endwhile; ?>
</tr>
</tbody>
</table>
</div>
</body>
</html>https://stackoverflow.com/questions/52605787
复制相似问题