Fetch not returning anything [PDO::FETCH_ASSOC]

Asked

Viewed 133 times

0

Connectionclass

<?php
abstract class ConnectionClass {
    protected function conecta(){
        $dsn = "mysql:host=localhost; dbname = coletase"; 
        $user = 'root'; 
        $pass = '';       
        try{
            $connectionPDO = new PDO($dsn, $user, $pass);
            echo "foi";
            return $connectionPDO;
        }catch(PDOException $erro){
            echo "DEU ERRO";
            echo ($erro->getMessage());
        }
    }
}  

My code:

<?php
include("conexao.php");
class PegaDadosClass extends ConnectionClass{
    //Exibição dos dados dos locais em JSON
    public function cospeJSON(){
        $fetching=$this->conecta()->prepare("select * from local");
        $fetching->execute();

        $j=[];
        $i=0;

        while($fetch=$fetching->fetch(PDO::FETCH_ASSOC)){
            $j[$i]=[
                "id_local"=>$fetch['id_local'],
                "latitude"=>$fetch['latitude'],
                "longitude"=>$fetch['longitude'],
                "descricao"=>$fetch['descricao'],
                "titulo"=>$fetch['titulo'],
            ];
            $i++;
        }

        header("Access-Control-Allow-Origin:*");
        header("Content-type: application/json");
        echo json_encode($j);
    }
}

It returns me an empty array. The connection is established normally, but for some reason it returns me an empty array. Can you help me? I really have no idea why it didn’t work.

  • Instead of manually fetch each one tries to call this right after the run(), $result = $fetching->fetchAll(PDO::FETCH_ASSOC); and see what he brings

1 answer

0

Good morning,

I was going through the same problem, I identified that my main method was going through an array, where I was deleting a certain data, later I tried to recover the deleted data, just as I was inside the same transaction returned a Null value in the array, even when I took the query that returned a result, because inside the transaction was already deleted and when the error occurred I sent a rollback.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.