Error in obtaining bank value

Asked

Viewed 25 times

2

I’m trying to get the ID of a database with PHP. When returning the value, it always comes as 1.

//Obtenho os valores por GET
$description = $_GET['DESC'];
$contentType = $_GET['TYPE'];
$content = $_GET['CONT'];

//Crio meu objeto de acesso ao banco e realizo a query
$dao = new DAO();
$result = $dao->select("SELECT ID_PUBLICATION FROM TB_PUBLICACOES_E_EVENTOS WHERE TXT_DESCRIPTION = :description AND TXT_TYPE = :contentType AND TXT_CONTENT = :content", Array(":description"=>$description, ":contentType"=>$contentType, ":content"=>$content));

//Atribuo o retorno (array) para um int 
$id_publication = (int) $result;

//Exibo os dois valores na tela
var_dump($id_publication);
var_dump($result);

The result appears to me like this:

int(1)
array(1) {
  [0]=>
  array(1) {
    ["ID_PUBLICATION"]=>
    string(1) "8"
  }
}

From what I understand, it is returning the number of positions of the array and assigning the value to my $id_publication, so it’s always 1.

It is possible to access this value that is returned from the bank?

1 answer

2


$result is an array returned by a fetchAll() this way it always comes with Index Zero. The correct way to access is:

$result[0]['ID_PUBLICATION'];
  • Wow, it was fast! hahaha. Thank you @rray, that’s exactly what was wrong! I need to wait 8 minutes to score as response.

  • 1

    @Csorgo Using the PDO? When returning only one records use the fetch() so you access the direct value (without the zero input)

  • Thanks for the tip! I will adapt my code.

Browser other questions tagged

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