我是PHP编程的新手,我需要您的帮助,如何为从数据库接收的信息创建一个表,或者如何从数据库中检索所有数据?一次,没有为每行信息重写该表? 这是我的代码和图片:
<?php
include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta charset="utf-8">
<title>mysql connect</title>
</head>
<body>
<?php
$sql = "SELECT * FROM users;";
$results = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($results);
if ($resultCheck > 0){
while ($row = mysqli_fetch_assoc($results)) {
echo '<center><table class="table table-dark"><thead><tr><th scope="col">#</th><th scope="col">Nume</th></tr></thead><tbody><tr><th scope="row">#</th><td>'.$row['user_last']."</td></tr></tbody></table></center> ";
}
}
?>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</html>
答案 0 :(得分:1)
您只需要循环tr
而不是整个表。像下面一样
<?php
include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta charset="utf-8">
<title>mysql connect</title>
</head>
<body>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nume</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM users;";
$results = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($results);
if ($resultCheck > 0){
$i=1;
while ($row = mysqli_fetch_assoc($results)) {
echo '<tr><th scope="row">'.$i.'</th><td>'.$row['user_last']."</td></tr>";
$i++;
}
}
?>
</tbody>
</table>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</html>
答案 1 :(得分:0)
在您的while循环中尝试一下
<th>#</th>
<th>firstname</th>
<?php
Echo "<tr>
<td>{$row['id']}</td>
<td>{$row['firstname']}</td>
</tr>";
?>
如果这不起作用,请尝试在id和firstname的索引后面放置半冒号。希望这会有所帮助