PHP - Si la tabla esta vacía que salga un mensaje de que no hay información

 
Vista:

Si la tabla esta vacía que salga un mensaje de que no hay información

Publicado por arnau (1 intervención) el 20/02/2021 20:14:13
Buenas, tengo un código HTML que vendría a ser el formulario, el otro código PHP el que mostrare a continuación, es información que se muestra sacada de una base de datos PHPMyAdmin. La idea es que al pedir información de una tabla vacía salga un mensaje de que no hay información que vuelva a atrás, pero este mensaje me sale constantemente incluso cuando se que en la tabla que solicito hay información, es un if(isset), estoy aprendiendo ahora y seguro es una tontearía y no soy capaz de ver el error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
   <html>
  <head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
  <meta charset="UTF-8">
  <meta id="viewport" content="width=device-width,initial-scale=1.0">
  </head>
   <body>
 
   <?php
 
   $fecha_inscrip=$_GET["fecha_inscrip"];
   $fecha_inscrip2=$_GET["fecha_inscrip2"];
   $posicion=$_GET["posicion"];
   $equipo=$_GET["equipo"];
   $edad_min=$_GET["edad_min"];
   $edad_max=$_GET["edad_max"];
 
   $server = "localhost";
   $nombrebd = "examenphp";
   $usuario = "root";
   $passwd = "";
 
   $conect=mysqli_connect($server,$usuario,$passwd,$nombrebd);
 
 
 
 
  $consul2=
  "SELECT J.NOMBRE, E.EQUIPO, J.POSICION, J.EDAD, J.FECHA_INSCRIP, J.DISP_JUGAR
  FROM CONTRATO C
  INNER JOIN JUGADORES J
  ON C.ID_JUGADOR = J.ID_JUGADOR
  INNER JOIN EQUIPO E
  ON C.ID_EQUIPO = E.ID_EQUIPO
  WHERE (E.EQUIPO = '$equipo')
AND (J.POSICION = '$posicion')
AND (J.FECHA_INSCRIP > '$fecha_inscrip')
AND (J.FECHA_INSCRIP < '$fecha_inscrip2')
AND (J.EDAD > '$edad_min')
AND (J.EDAD < '$edad_max')";
 
 
   $resultado=mysqli_query($conect,$consul2);
 
 
   ?>
 
   <?php
      if(isset($tabla2)){
 
 
 
      ?>
 
      <h2>Los datos de los jugadores</h2>
 
    <table>
 
       <br>
 
      <tr>
 
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Nombre del Jugador</th>
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Equipo que pertenece</th>
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Posicion que Juega</th>
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Edad</th>
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Fecha que se unio al equipo</th>
        <th style="padding: 5px; text-align: center; border: 1px solid black;">Disponibilidad para jugar</th>
      </tr>
 
       <?php
      while ($tabla2 = mysqli_fetch_row($resultado)){
 
 
      ?>
 
     <tr>
 
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php echo $tabla2[0]?></td>
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php echo $tabla2[1]?></td>
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php echo $tabla2[2]?></td>
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php echo $tabla2[3]?></td>
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php echo $tabla2[4]?></td>
       <td style="padding: 5px; text-align: center; border: 1px solid black;"><?php
        if($tabla2[5] !=0){
          echo 'Si';
        } else {
          echo 'No';
        } ?></td>
 
     </tr>
     <?php
      }
 
      ?>
 
   </table>
<?php
}else{
  echo "No hay datos de los jugadores, vuelva a <a href='formulario.html'>atrás</a>";
}
?>
 
 
 
 
   </body>
   </html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de joel
Val: 3.828
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Si la tabla esta vacía que salga un mensaje de que no hay información

Publicado por joel (1269 intervenciones) el 21/02/2021 09:36:13
Hola Arnau, te recomiendo que tabules correctamente tu código, he tenido que copiarlo praa ver donde empiezan y acaban los if()

en la linea 50 tienes:
1
if(isset($tabla2)){
pero... que es $tabla2?? donde se define?

Yo creo que ahi deberia haber algo así:
1
if ($resultado) {

Puedes probarlo y comentar?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar