2016-12-10 3 views
-1

Я пытаюсь отобразить данные из базы данных, я не совсем понимаю все процессы, которые php следует для получения и подготовки информации. Дело в том, что кажется, что я не нагружают, что в переменной $filasНедопустимый аргумент для foreach() PDO PHP

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; 
    } 


} 
?> 

Я также попытался ввести array в foreach, и он не указал на ошибку t o я, но он не показывал никакой информации, все в белом И теперь, если кто-то может объяснить мне, как захватить по ошибке, отлично.

   </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; 
    } 
} 
?> 
+0

Что такое новый conexion(); – RiggsFolly

+0

Я думаю, что 'cargarDatos()' терпит неудачу и возвращает '$ row = null;' – RiggsFolly

+0

Предложите переместить 'if (! Statement) {' block непосредственно после 'prepare' – RiggsFolly

ответ

0

Я думаю, что петля, пока нет необходимости, используйте 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; 
    } 

Тогда:

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

PHP Doc fetchAll()

+0

Большое спасибо, я этого не представлял. Спасибо другим – RubenStryker

Смежные вопросы