Table that does not load with php

1

I have a page where the intention is to upload a series of messages received by a user. For that in the principle of loading the page I do the following:

<?php
$conexion = new Conexion();
$sql = "SELECT id, fecha_hora, remitente, asunto FROM mensajes WHERE destinatario = :valor";
$stmt = $conexion -> prepare($sql);
$stmt->bindParam(':valor', $_SESSION['id_usuario']);
$stmt->execute();
?>

Below, in the body of the page I do the following:

<table class="ui padded stackable table">
<thead >
    <tr>
        <th hidden>Id</th>
        <th>Fecha</th>
        <th>Remitente</th>
        <th>Asunto</th>
    </tr>
</thead>

<tbody>
    <?php while($resultado = $stmt->fetch()) { ?>
    <tr>
        <td hidden><?php echo $resultado['id'];?></td>
        <td><?php echo $resultado['fecha_hora'];?></td>
        <td><?php echo $resultado['remitente'];?></td>
        <td><?php echo $resultado['asunto'];?></td>
    </tr>
    <?php } ?>
</tbody>

The strangest thing of all this is that the same structure I make it in other pages with other tables that I fill with the same system but in this case it does not load anything. I made several modifications to see that the data is loaded, in fact I did the happy while following where I do the $ stmt-> execute () and I have placed two echoes to show the records and I SHOW THEM !!! ... now because he does not show them in the table, I do not understand ...

Look at the following: I do this in the header of the page and it returns the bottom image:

$conexion = new Conexion();
$sql = "SELECT id, fecha_hora, remitente, asunto FROM mensajes WHERE destinatario = :valor";
$stmt = $conexion->prepare($sql);
$stmt->bindParam(':valor', $_SESSION['id_usuario']);
$resultado = $stmt->execute();

while($resultado = $stmt->fetch()) {
    echo $resultado['id'] . "<br>";
    echo $resultado['fecha_hora'] . "<br>";
    echo $resultado['remitente'] . "<br>";
    echo $resultado['asunto'] . "<br>";
}

HOW IS THIS POSSIBLE ????

    
asked by MNibor 04.08.2017 в 19:10
source

2 answers

2

I would solve it in this way. Assuming the query works and returns data.

The variable $html also concatenates the other tags in your table, before entering the loop. When you exit you close the tbody and the table.

$stmt ->execute();
//Creamos array con los datos
$arrDatos = $stmt->fetchAll(PDO::FETCH_ASSOC);

/**
  * Preguntamos si la consulta devolvió datos y si es así, los imprimimos
  * Otro aspecto interesante de PDO es que no es necesario usar rowCount
  * para saber si la consulta devolvió datos,
  * basta con verificar los mismos datos con un if, ya que si no devuelve nada
  * el valor de $arrDatos sería FALSE
 */  

if ($arrDatos)
{          

   /**
     * En la variable $html iremos concatenando una tabla
     * que se llenará  con los  resultados  de la  consulta
   */  

    $html="<table>";
    foreach ($arrDatos as $row)
    {
        $html.= '<tr>';
        $html.= '<td>'.$row["id"].'</td>';
        $html.= '<td>'.$row["fecha_hora"].'</td>';
        $html.= '<td>'.$row["remitente"].'</td>';
        $html.= '<td>'.$row["asunto"].'</td>';
        $html.= '</tr>';

    }
    $html.="</table>";
    /* Imprimimos la  tabla */
    echo $html;

}
else
{
    echo "No hay datos";
}
    
answered by 04.08.2017 / 20:09
source
0

Hello the variable $ result I do not see that you are filling with the information of the query, try this:

<?php
$conexion = new Conexion();
$sql = "SELECT id, fecha_hora, remitente, asunto FROM mensajes WHERE destinatario = :valor";
$stmt = $conexion -> prepare($sql);
$stmt->bindParam(':valor', $_SESSION['id_usuario']);
$resultado = $stmt->execute();
?>

So that the variable $ result contains data and now if you make the journey through the while, you tell us how it went and continue to help you.

    
answered by 04.08.2017 в 19:15