W
W
wolverine7772021-01-30 17:20:42
PHP
wolverine777, 2021-01-30 17:20:42

How to display a string if the database is empty?

Hi, I have a page that displays ratings for music albums. Accordingly, for some there is an assessment, and for some it is not.

I, as a beginner, was able to reach the object and display the ratings that exist in the database, but for those albums in which it is empty - my message is displayed, but at the same time an error appears that "there is no information" (see screenshot)

60156ac602d3e530822262.png

Like this remove error? if else doesn't help :(((

Thanks!

<?php

// Connection to a database

  $connect=connection();	

// SQL request 1 - for ALBUMS

  $sql="
  select * 
  from album
  ";	

// Execution 1

    $result=$connect->query($sql);

// SQL request 2 - for ARTISTS

  $sql2="
  select * 
  from artiste
  ";	

// Execution 2
    $result2=$connect->query($sql2);

// SQL request 3 - for NOTES

  $sql3="
    SELECT 
        idOeuvre, 
        ROUND(AVG(note)) AS average
    FROM noter
    GROUP BY idOeuvre
  ";	

// Execution 3

  $result3=$connect->query($sql3);

// Loop to arrange results in an object

?><div class="row">

<table >
    <thead>
        <tr>
            <th >TITRE</th>
            <th >Artiste</th>
            <th >Note moyenne</th>
        </tr>
    </thead>

<?php while($albums=$result->fetch(PDO::FETCH_OBJ)) { ?>    

    <tbody>
        <tr>
            <td><a href="description.php?choice=<?php echo $albums->id; ?>"><h5 class="card-title"><?php echo $albums->Titre; ?></h5></a></td>
            <td><h5 class="card-title"><?php $artists=$result2->fetch(PDO::FETCH_OBJ); echo $artists->Nom; ?></h5></td>
            <td><h5 class="card-title">
      
      <?php $notes=$result3->fetch(PDO::FETCH_OBJ); 
      
      $av = $notes->average;
      
      if ($av!=null) {
        echo $notes->average;
      } 
      else {
        echo "pas de note...";
      } ?></h5>
      
      </td>
      
        </tr>
    </tbody>

<?php } ?>

</table>

</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
no_one_safe, 2021-01-30
@wolverine777

You don't need to check $av for null, but $notes. And it's better not null, but is_object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question