while in infinite loop using $ result = $ result-fetch (PDO :: FETCH_ASSOC);

1
<?php include ('pdo.php');
class crud
{
    public static function select($arg){
        $pdo = new pdoinit();
        $result = $pdo->prepare($arg);
        $result->execute();
        $resultado = $result->fetch(PDO::FETCH_ASSOC);
        return $resultado;
    }
}
$arr = crud::select('select * from grupos');
print_r($arr);
while ($row = $arr){
    echo $row['nome'];
}
    
asked by anonymous 14.11.2016 / 16:01

1 answer

3

There are two errors in your code.

The first mistake is simple, you are copying infinitely all content from $arr to $row , one solution would be to use foreach .

foreach($arr as $row) {
   echo $row['nome'];
}

Let's go to the second error, note that you are using fetch in line $resultado = $result->fetch(PDO::FETCH_ASSOC); which will return only one line at a time, so result will have an associative array with the first line, I recommend using fetchAll so this line would look like this:

    $resultado = $result->fetchAll(PDO::FETCH_ASSOC);
    
15.11.2016 / 05:37