我想在MySQL数据库中检索信息并显示它的html表。当我尝试使用此代码时,我收到错误消息。我无法解决这个问题。我是php.help的新手。这是我的代码
<?php
session_start();
?>
<?php
$conn = mysqli_connect("localhost","root","","doctor");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['button'])) {
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Approvals</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<br>
<br>
<div class="table-responsive">
<table id="mytable" class="table table-bordred table-striped" border="1">
<thead style="background-color: sandybrown">
<th><input type="checkbox" id="checkall" /></th>
<th>Doctor ID</th>
<th>Doctor Name</th>
<th>Payment Type</th>
<th>Mobile Number</th>
<th>Email</th>
<th>Pay Date</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="checkthis" /></td>
<?php
while( $row = mysqli_fetch_assoc($sql)): ?>
<td > <?php echo $row['Mid']; ?> </td >
<td > <?php echo $row['Mname']; ?></td >
<td > <?php echo $row['Mnic']; ?> </td >
<td > <?php echo $row['amount']; ?></td >
<td > <?php echo $row['month']; ?> </td >
<td ><?php echo $row['bank']; ?></td >
</tr >
<?php endwhile ?>
</tbody>
</table>
<div class="form-group " align="center">
<a style="box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);border: 2px solid #4CAF50;background-color: white;color: black;width: 15%; float: right" href="#" target="_blank" type="button" id="button" name="button" class="btn btn-primary btn-lg btn-block login-button">Submit</a>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
您过早关闭了连接。在迭代项目时必须打开连接。
答案 1 :(得分:1)
我同意其他答案,但在此之前你需要实际查询。
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
只是if block
中的一个字符串。
设置
$sql="";
外部。
if (isset($_POST['button'])) {
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
}
并致电
$res = mysqli_query($conn,$sql);
和
while( $row = mysqli_fetch_assoc($res))
答案 2 :(得分:0)
删除此行:
conn->close();
如果不起作用,请明确指出该错误。
并在
后结束后添加此行<?php endwhile;
mysqli_close();
?>