关闭
doujia4041 2016-12-10 08:20
浏览 42
已采纳

为foreach()PDO PHP提供的参数无效

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条回答 默认 最新

  • douhanshu5517 2016-12-10 08:55
    关注

    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()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部