Warning: Invalid argument supplied for foreach () when passing a resultset

0
  

Warning: Invalid argument supplied for foreach () in   C: \ xampp \ htdocs \ course_php \ pdo.php on line 9

My code is:

<?php 

try {
    $conexion = new PDO('mysql:host=localhost;dbname=curso', 'root', '');
    echo "conexion exitosa";

    $resultado = $conexion->query ( 'SELECT * FROM curso');

    foreach ($resultado as $fila) {
        print_r($fila);
       }

} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage();
} >
    
asked by Paul Brito 28.09.2018 в 22:10
source

1 answer

2

$ result is a "pointer" to the resultset obtained with the query, to access the rows you must do it through the fetch functions eg.

while($fila = $resultado->fetch()) {
        echo $row['nombreColumna'];// Y asi con cada columna
    }
    
answered by 29.09.2018 в 00:14