null值

hrirmatl  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(375)

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

<?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

fiei3ece

fiei3ece1#

这个 where 查询的子句会过滤所有4列都为空的行,这似乎是您想要的。
但是你的 select 子句只生成一列,第一个非空值跨四列。您可能不希望这样,因为您的代码试图获取每一列并在表中显示它们。
所以:

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

相关问题