提问者:小点点

从数据库中提取数据时显示空值


我想从数据库中获取数据,但是数据库中包含许多null值。 每一行正好包含四个null值。 我想那些空值应该被删除,同时在一个网页获取。 我有这条路

<?php
session_start();
include('database.php');
$sql="SELECT COALESCE(physics8, chemistry8, maths8, biology8, english8) 
FROM class8 
WHERE teacher_id = :id 
AND COALESCE(physics8, chemistry8, maths8, biology8, english8) IS NOT NULL";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $_SESSION['teacher_id']);
$result = $stmt->execute();

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link href="css/teacherdashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
    <title>teacher dashboard</title>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

</head>
<style>
    td
    {
        font-family: sans-serif;
        text-align: center;

        font-weight: bold;
    }

    th
    {
        text-align: center;
    }
    </style>
<body>


<nav class="navbar navbarheight navbar-expand-lg navbar-white bg-white">
<a class="navbar-brand" href="teacherdashboard.php"><img  class="logo" height="20%" width="50%" srcset="image/logo1.png" border="0" alt="Null"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
       <button type="button" onclick="window.location.href='teacherlogout.php';" class="student  btn btn-danger">Logout</button>
      </li>
      </li>
    </ul>
  </div>
</nav>

<div style="margin-top: 10%;" class="container tablediv">
<table class="table table-bordered table-condensed table1 ">
 <thead class="thead-dark">
    <tr>
         <th class="col2">physics</th>
         <th class="col2">chemistry</th>
         <th class="col2">maths</th>
         <th class="col2">biology</th>
         <th class="col2">english</th>
    </tr>
 </thead>
 <?php  while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { ?>
    <tr>

         <td> <?php echo $row->physics8 ?></td>
         <td> <?php echo $row->chemistry8 ?></td>
         <td> <?php echo $row->maths8 ?></td>
         <td> <?php echo $row->biology8 ?></td>
         <td> <?php echo $row->english8 ?></td>

    </tr>
 <?php } ?>
 </table>
</div>
<?php include('footer.html') ?>
</body>
</html>

但我有一些很奇怪的错误。 我不明白。。。请检查错误https://postimg.cc/jywdcqrn


共1个答案

匿名用户

查询的where子句确实过滤了所有4列都为空的行,这似乎是您想要的。

但是select子句只生成一列,四列中的第一个非NULL值。 您可能不希望这样做,因为您的代码试图获取每一列并将它们显示在一个表中。

所以:

SELECT physics8, chemistry8, maths8, biology8, english8
FROM class8 
WHERE teacher_id = :id 
AND COALESCE(physics8, chemistry8, maths8, biology8, english8) IS NOT NULL