当MySQL数据库中没有图像“ BLOB”时,我想显示“无图像”

时间:2019-11-25 07:55:27

标签: php mysql blob

基于上面的问题,当前,当数据库中没有图像时,它将显示损坏的图像,而如果有图像,则将显示图像。现在,当MySQL数据库中没有图像“ BLOB”时,如何显示“无图像”。

我在MySQL数据库中使用BLOB格式。下面是代码:

<?php

$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){

  echo "<table class = 'table-bordered' width ='100%'>";
  echo "<thead>";
    echo "<tr>";
        echo "<th>Date</th>
        <th>Task Name</th>
        <th>Before Task</th>
        <th>After Task</th>
        <th>OT From</th>
        <th>OT To</th>
        <th>Status</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody >
    <tr>
    <td colspan='8'>No report at this moment</td>
    </tr>
    </tbody>
    </table>";
}else{

      echo "<table class = 'table-bordered' width ='100%'>";
      echo "<thead>";
        echo "<tr>";
            echo "<th>Date</th>
            <th>Task Name</th>
            <th>Before Task</th>
            <th>After Task</th>
            <th>OT From</th>
            <th>OT To</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody >";

        $query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
        while($row = $query->fetch(PDO::FETCH_ASSOC)){
        echo "<tr>";

          echo "<td rowspan='2'>". $row['report_date'] . "</td>";
          echo "<td rowspan='2'>". $row['task_name'] . "</td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
          echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
          echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
          echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
          echo "<td rowspan='2' align='center'>";
            echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
            echo "</td>";

        echo "</tr>";

        echo "<tr>";

          echo "<td align='center'>". $row['time_photo_before'] . "</td>";
          echo "<td align='center'>". $row['time_photo_after'] . "</td>";

        echo "</tr>";
        }

        echo "</tbody>";
      echo "</table><br>";  
    echo "</div>";
  echo "<div>";
}
?>            

2 个答案:

答案 0 :(得分:0)

只需设置一个条件来检查图像在行中是否存在。

if(!isset($row['photo_after']) || empty($row['photo_after'])) {
   echo "<td align='center'><img src='default.png'/></td>";
}
else 
{ 
   echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>"; 
}

答案 1 :(得分:-1)

尝试一下...,我希望这对您有用。 在您的while循环内,我创建了一个条件来检查您的字段中是否有内容或该内容是否为空。如果为空,则变量将存储要打印的文本;如果不为空,则它将在变量$ image中显示图像,并且在要显示图像的位置回显该变量。

<?php

$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){

  echo "<table class = 'table-bordered' width ='100%'>";
  echo "<thead>";
    echo "<tr>";
        echo "<th>Date</th>
        <th>Task Name</th>
        <th>Before Task</th>
        <th>After Task</th>
        <th>OT From</th>
        <th>OT To</th>
        <th>Status</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody >
    <tr>
    <td colspan='8'>No report at this moment</td>
    </tr>
    </tbody>
    </table>";
}else{

      echo "<table class = 'table-bordered' width ='100%'>";
      echo "<thead>";
        echo "<tr>";
            echo "<th>Date</th>
            <th>Task Name</th>
            <th>Before Task</th>
            <th>After Task</th>
            <th>OT From</th>
            <th>OT To</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody >";

        $query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
        while($row = $query->fetch(PDO::FETCH_ASSOC)){


      // Here is my code...



          if(empty($row['photo_after'])) {
               $image = "NO IMAGE FOUND..."; //anything you want to diaplay if there is no image
             }
             else{ $image = "<img src='data:image/jpeg;base64,".$row['photo_after']."'/>"; }





     // Check the $image variable in your table




        echo "<tr>";

          echo "<td rowspan='2'>". $row['report_date'] . "</td>";
          echo "<td rowspan='2'>". $row['task_name'] . "</td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
          echo "<td align='center'>".$image."</td>";   // $image store your data
          echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
          echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
          echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
          echo "<td rowspan='2' align='center'>";
            echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
            echo "</td>";

        echo "</tr>";

        echo "<tr>";

          echo "<td align='center'>". $row['time_photo_before'] . "</td>";
          echo "<td align='center'>". $row['time_photo_after'] . "</td>";

        echo "</tr>";
        }

        echo "</tbody>";
      echo "</table><br>";  
    echo "</div>";
  echo "<div>";
}
?>