Display data in a table with PDO

3

I HAVE PROBLEMS WHEN SHOWING THE DATA I DO NOT KNOW HOW TO DO IT WITH PDO ALREADY LOOK AT VARIOUS EXAMPLES BUT I DO NOT FIND ONE THAT SERVES ME

<?php

$conexion=new PDO("mysql:host=127.0.0.1;dbname=eladeria","root","");

    $busqueda=$conexion->prepare("Select * from ventas");
    $busqueda->execute();

?>

<table   class="table table-bordered">
    <th class="bg-primary" scope="col">Id</th>
    <th class="bg-primary" scope="col">producto</th>
    <th class="bg-primary" scope="col">precio</th>
    <th class="bg-primary" scope="col">cantidad</th>
    <th class="bg-primary" scope="col">Fecha de venta</th>
    <?php

   /* var_dump($busqueda);*/
    while ($muestra = fetchAll($busqueda)) {
        echo '<tr>';

        echo '<td >' . $muestra['id'] . '</td>';
        echo '<td >' . $muestra['producto'] . '</td>';
        echo '<td >' . $muestra['precio'] . '</td>';
        echo '<td >' . $muestra['cantidad'] . '</td>';
        echo '<td >' . $muestra['fecha_cantidad'] . '</td>';

        echo '<td>
                  <a class="btn btn-danger ajax-request" id="eliminar"  data-target="'.$muestra['id_persona'].'">   
                  <i   class="fas fa-trash"></i>
                  </a>

                 </td>';
        echo ' </tr>';

    }
    ?>

</table>
    
asked by user9472850 02.08.2018 в 03:31
source

2 answers

2

Your code is erroneous and incoherent .

It's erroneous because fetchAll is a method of the PDOStatement object, that is, the object that is created with prepare or with query . In your case that object is $busqueda , therefore to invoke fetchAll you must use your object, applying the call style: $objeto->metodo(parametros) . In addition, what fetchAll should receive in parameter is fetch_style . Also, it is not for use in while , as is the case with fetch ( see here for more details ) because he gives you back all the information that the query brings. Instead of constantly invoking fetchAll in while , it is correct to store your result in a variable and traverse that variable to show the data (see the use of $arrDatos in the code below).

It's incoherent because your query does not need to be prepared, since it does not handle external data. You can use query in this case.

I suggest you try like this:

$conexion=new PDO("mysql:host=127.0.0.1;dbname=eladeria","root","");

    $busqueda=$conexion->query("Select * from ventas");
    /*Almacenamos el resultado de fetchAll en una variable*/
    $arrDatos=$busqueda->fetchAll(PDO::FETCH_ASSOC);

?>

<table   class="table table-bordered">
    <th class="bg-primary" scope="col">Id</th>
    <th class="bg-primary" scope="col">producto</th>
    <th class="bg-primary" scope="col">precio</th>
    <th class="bg-primary" scope="col">cantidad</th>
    <th class="bg-primary" scope="col">Fecha de venta</th>
    <?php

   /* var_dump($arrDatos);*/
   /*Recorremos todos los resultados, ya no hace falta invocar más a fetchAll como si fuera fetch...*/
   foreach ($arrDatos as $muestra) {
        echo '<tr>';

        echo '<td >' . $muestra['id'] . '</td>';
        echo '<td >' . $muestra['producto'] . '</td>';
        echo '<td >' . $muestra['precio'] . '</td>';
        echo '<td >' . $muestra['cantidad'] . '</td>';
        echo '<td >' . $muestra['fecha_cantidad'] . '</td>';

        echo '<td>
                  <a class="btn btn-danger ajax-request" id="eliminar"  data-target="'.$muestra['id_persona'].'">   
                  <i   class="fas fa-trash"></i>
                  </a>

                 </td>';
        echo ' </tr>';

    }
    ?>

</table>
    
answered by 02.08.2018 / 03:50
source
1

I greet you and I tell you the following, first I would leave the exercise in this way (which I have already tried and it is functional)

<?php

$conexion=new PDO("mysql:host=127.0.0.1;dbname=blog;port=3307","root","pass");

    $busqueda=$conexion->prepare("Select * from users");
    $busqueda->execute();
    $resultado = $busqueda->fetchAll();

?>

<table   class="table table-bordered">
   <tr>
      <th class="bg-primary" scope="col">Id</th>
      <th class="bg-primary" scope="col">nameUser</th>
      <th class="bg-primary" scope="col">passwordUser</th>
      <th class="bg-primary" scope="col">statusUser</th>
      <th class="bg-primary" scope="col">created_at</th>
      <th class="bg-primary" scope="col">address</th>
   </tr>
    <?php
      foreach($resultado as $res)
      {
        echo "<tr>";
        echo "<td>".$res["idUser"]."</td>";
        echo "<td>".$res["nameUser"]."</td>";
        echo "<td>".$res["passwordUser"]."</td>";
        echo "<td>".$res["statusUser"]."</td>";
        echo "<td>".$res["created_at"]."</td>";
        echo "<td>".$res["address"]."</td>";
        echo "</tr>";
      }   
    ?>
</table>
  

As you can see I do not put fetchAll() in the loop, but out   I do it where $resultado receives the search value and this same one to its   once you access fetchAll () that will convert it into an array which   Then you can go ideally through the foreach loop which I   helps to traverse the elements assigning the value in each iteration to   an alias that in this case is $res

The foreach is useful because, for example, if you do a $var_dump($resultado); you will see that it brings you an array in the form of a key value, which means that in order to make the foreach values, it is not more useful

Note if some data does not seem to be familiar with your exercise, it is because you replicate it with your own to find the reason for your failure.

  

As you can see yourself, from the official PHP documentation,   In the following link    link

     

the fetchAll() method itself already returns the complete array of   query data; then it is no longer necessary to put it in the loop

    
answered by 02.08.2018 в 03:56