doujia4041 2016-12-10 16: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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 关于#C语言#的问题,如何解决?
      • ¥20 Vs2017 Help Viewer2.3 问题
      • ¥35 基于嵌入式linux的日程管理软件
      • ¥50 如何将list字符串添加到CSV文件表头?
      • ¥15 关于#javascript#的问题:通过ajax实现的局部刷新 如何将项目打包
      • ¥15 海思uboot USB3.0无法识别
      • ¥15 无法调用库文件,自己可以找到,但编译时显示没有
      • ¥15 安装PyQt5的时候这里创建虚拟环境是哪里?具体是怎么的?能录个视频吗
      • ¥20 php程序设计题不会!求解答!
      • ¥50 关于#python#的问题,如何解决?