-1

I am trying to display data from a database, I do not quite understand all the processes that php follows to get and prepare information. The case is that it seems that I do not load what is in the variable $filas

Error: Warning: Invalid argument supplied for foreach() in /home/u571414187/public_html/systems/EazyFive/Controlador/cargar.php on line 20

Cargar.php

<?php

function cargar(){
    $consultas = new consultas();
    $filas = $consultas->cargarDatos;

    echo "<table>
            <tr>
                <th>ID</th>
                <th>Nombre</th>
                <th>Pass</th>
                <th>Canal</th>
                <th>Puntuacion</th>
                <th>url_img</th>
                <th>url_perfil</th>
                <th>logros</th>
            </tr>";

    foreach ($filas as $fila) {
        echo "<tr>";
        echo "<td>".$fila['id']."</td>";
        echo "<td>".$fila['user']."</td>";
        echo "<td>".$fila['pass']."</td>";
        echo "<td>".$fila['canal']."</td>";
        echo "<td>".$fila['puntuacion']."</td>";
        echo "<td>".$fila['url_img']."</td>";
        echo "<td>".$fila['url_perfil']."</td>";
        echo "<td>".$fila['logros']."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>

class.consultas.php

<?php

class consultas{
    public function insertarProducto($arg_user, $arg_pass, $arg_canal, $arg_puntuacion, $arg_urlimg, $arg_urlperfil, $arg_logros){
        $modelo = new conexion();
        $conexion = $modelo->get_conexion();
        $sql = "insert into EzyFive_users (user, pass, canal, puntuacion, url_img, url_perfil, logros) values (:user, :pass, :canal, :puntuacion, :url_img, :url_perfil, :logros)";
        $statement = $conexion->prepare($sql);
        $statement->bindParam(':user', $arg_user);
        $statement->bindParam(':pass', $arg_pass);
        $statement->bindParam(':canal', $arg_canal);
        $statement->bindParam(':puntuacion', $arg_puntuacion);
        $statement->bindParam(':url_img', $arg_urlimg);
        $statement->bindParam(':url_perfil', $arg_urlperfil);
        $statement->bindParam(':logros', $arg_logros);  

        if(!statement){
            return "Error al crear el registro";
        }
        else{
            $statement->execute();
            return "Registro creado correctamente";
        }

    }

    public function cargarDatos(){
        $row = null;
        $modelo = new conexion();
        $conexion = $modelo->get_conexion();
        $sql = "select * from EzyFive_users";
        $statement = $conexion->prepare($sql);
        $statement->execute();
        while($resultado = $statement->fetch()){
            $rows[] = $result;
        }
        return $rows;
    }


}
?>

I have also tried to enter array in the foreach, and it did not indicate any error to me, but it did not show any information, everything in white And now if someone can explain me how to capture in error, perfect.

                </tr>";

    foreach ((array) $filas as $fila) {
        echo "<tr>";

Conexion.PHP

<?php

class conexion{
    public function get_conexion(){
        $user = "user";
        $pass = "pass";
        $host = "localhost";
        $db = "db";

        $conexion = new PDO("mysql:host=$host;dbname=$db;", $user, $pass);
        return $conexion;
    }
}
?>

1 Answers1

0

I think the while loop is not necessary, use fetchAll():

public function cargarDatos(){
        $modelo = new conexion();
        $conexion = $modelo->get_conexion();
        $sql = "select * from EzyFive_users";
        $statement = $conexion->prepare($sql);
        $statement->execute();
        $result = $statement->fetchAll();
        return $result;
    }

Then:

$filas = $consultas->cargarDatos();
foreach ($filas as $fila) {

PHP Doc fetchAll()

kip
  • 1,120
  • 7
  • 11