Problems with PDO in php

2
Good afternoon, I'm trying to access my database to get the 12 countries present in the table and return an array with the id and name of the 12 countries but my code is only returning 1 position in the array that is the first country only Anyone could help me please. Thanks in advance.

function recuperaPaises(){
    //Função que pega os países do banco e jogam no array de países da classe
    $conexao = new DB;
    $conexao=$conexao->getConnection();
    $rs = $conexao->prepare("SELECT * FROM paises");
    if ($rs->execute()){

  $row = $rs->fetch(PDO::FETCH_OBJ);
  return $row;

}
else{

  $flash="OPS!... ouve algum erro em nosso Sistema. Por Favor contate administrador!";
  echo $flash;
}

}
    
asked by anonymous 04.09.2017 / 18:06

1 answer

3

To return all the items in a query use the method fetchAll () , because if you only put fetch () is to return only one item of the result, usually in search of data of 1 customer, data of a purchase, etc, ie to bring a collection

:

Modified code:

function recuperaPaises()
{
    //Função que pega os países do banco e jogam no array de países da classe
    $conexao = new DB;
    $conexao=$conexao->getConnection();
    $rs = $conexao->prepare("SELECT * FROM paises");
    if ($rs->execute()){

      $row = $rs->fetchAll(PDO::FETCH_OBJ);
      return $row;

    }
    return null;
}

I made a modification if it did not return result return null out of this method call if the return information can be better worked.

04.09.2017 / 18:09