problems with infinite loop using PDO :: FETCH_ASSOC

1

My Query

$sql = "SELECT * FROM tablename WHERE algumvalor=2 LIMIT 1";

You are entering an infinite loop here

How do I use while ?

  while($row_data=$conn->executaQuery($sql)) 
  {
      var_dump($row_data);    
  }

With for works

$row_data = $conn->executaQuery($sql);


foreach($row_data  AS $dados){

    var_dump($dados);

}

Connection class and search

public function executaQuery($sql){
    $db = $this->conn;
    $sth = $db->prepare($sql);
    $sth->execute();
    return $sth->fetch(PDO::FETCH_ASSOC);
}
    
asked by anonymous 11.09.2015 / 23:50

1 answer

1

The foreach traverses all elements array of data that returns until the end and for when you have no more data.

Since while will always be doing the verification and the way you did it will always query again in the database, calling the function.

An example of how you could use while

$row_data = $conn->executaQuery($sql);
$i = 0;

while($i < count($row_data)) { 
    var_dump($row_data[$i]);
    $i++;
}
    
11.09.2015 / 23:56