Error showing list with PDO

2

Hello I'm trying to show a list which showed me with 'myslqi' now I'm working with PDO but I can not make it work the error that generates me I do not understand it.

function listadoUsuarios(){
    $db = new BaseDatos();
    $db->conectar();

    $sentencia = "SELECT * FROM Usuario";

    $db->prepare($sentencia);
    $sentencia->execute();

    echo "<div id='listado'>";
        echo "<table id='listadoUsuario' border=1 cellspacing=0>";
            echo "<tr>";
                echo "<th>".'Nombre completo'."</th>";  
                echo "<th>".'Correo'."</th>";  
                echo "<th>".'Teléfono'."</th>";  
                echo "<th>".'Dirección'."</th>";
                echo "<th>".'Fecha de nacimiento'."</th>";
            echo "</tr>";
            while ($row = $sentencia->fetch(PDO::FETCH_ASSOC)){     
                echo "<tr>";
                    echo "<td>".$row['nombre'].', '.$row['apellido']."</td>";  
                    echo "<td>".$row['email']."</td>";  
                    echo "<td>".$row['telefono']."</td>";
                    echo "<td>".$row['direccion'].', '.$row['cod_postal']."</td>";  
                    echo "<td>".$row['fecha_nac'].' - '."</td>";
                    echo "<td><button class='btn_tabla'><a href='google.es'>Modificar</a></button></td>";
                    echo "<td><button class='btn_tabla_mal'><a href='google.es'>Eliminar</a></button></td>";
                echo "</tr>";
            }  
        echo "</table>";
    echo "</div>";

}
    
asked by sergibarca 14.03.2018 в 11:54
source

2 answers

1

There are two problems in the code:

  • You are not retrieving the prepare and run result, in which you would have a result set that you could then read with one of the fetch methods later.

    Something like this:

    $resultado=$db->prepare($sentencia);
    $resultado=->execute();
    

    And then to read:

        while ($row = $resultado->fetch(PDO::FETCH_ASSOC)){    
    
  • From what you have said about the error Call to undefined method BaseDatos::query() , it can be seen that you are not recovering an instance of your PDO connection, which would be returned by the conectar method.

  • Also ...

    In this case you do not need to prepare the query, since in it there is nothing to prepare , because no data intervene from the outside.

    So, you can do it with query :

    function listadoUsuarios(){
        $pdo = new BaseDatos(); //Hay un cambio aquí
        $db=$pdo->conectar();   //Hay un cambio aquí
    
        $sentencia = "SELECT * FROM Usuario";
    
        $resultado=$db->query($sentencia); //Hay un cambio aquí
    
        echo "<div id='listado'>";
            echo "<table id='listadoUsuario' border=1 cellspacing=0>";
                echo "<tr>";
                    echo "<th>".'Nombre completo'."</th>";  
                    echo "<th>".'Correo'."</th>";  
                    echo "<th>".'Teléfono'."</th>";  
                    echo "<th>".'Dirección'."</th>";
                    echo "<th>".'Fecha de nacimiento'."</th>";
                echo "</tr>";
                while ($row = $resultado->fetch(PDO::FETCH_ASSOC)){ //Hay un cambio aquí    
                    echo "<tr>";
                        echo "<td>".$row['nombre'].', '.$row['apellido']."</td>";  
                        echo "<td>".$row['email']."</td>";  
                        echo "<td>".$row['telefono']."</td>";
                        echo "<td>".$row['direccion'].', '.$row['cod_postal']."</td>";  
                        echo "<td>".$row['fecha_nac'].' - '."</td>";
                        echo "<td><button class='btn_tabla'><a href='google.es'>Modificar</a></button></td>";
                        echo "<td><button class='btn_tabla_mal'><a href='google.es'>Eliminar</a></button></td>";
                    echo "</tr>";
                }  
            echo "</table>";
        echo "</div>";
    
    }
    
        
    answered by 14.03.2018 / 12:02
    source
    1

    The problem is that $sentencia is a string and does not have a prepare method. What you must do is assign the output of $db->prepare to a variable (which is a PDOStatement)

    $sentencia = "SELECT * FROM Usuario";
    
    $stmt = $db->prepare($sentencia);
    $stmt->execute();
    

    And then iterate using $stmt->fetch(PDO::FETCH_ASSOC) .

    That regards your code as you have it now. In case you are not using parameters for your query, A. Cedano is right, there is nothing to prepare.

        
    answered by 14.03.2018 в 12:12