Error "Notice: Undefined index:"

Asked

Viewed 379 times

-1

Hello! I’m getting this message while trying to display the page.

Notice: Undefined index: Identregator in C: wamp64 www Solus resume_dia.php on line 378

On line 378 I have the following structure.

375- $sqlEntregasRetornos_res = $conn->prepare($sqlEntregasRetornos);
376- $sqlEntregasRetornos_res->execute();        
377- $lstEntregasRet = $sqlEntregasRetornos_res>fetchAll(PDO::FETCH_ASSOC);
378- $teste = $lstEntregasRet["IdEntregador"];                      
379- var_dump($lstEntregasRet);

// Na linha 378 também tentei extrair da seguinte forma     
   $lstEntregasRet["IdEntregador"];

When displaying the contents of the variable it is correctly displaying the information. As below:

array (size=8)
  0 => 
    array (size=3)
      'IdEntregador' => string '4' (length=1)
      'QtdeRetorno' => string '4' (length=1)
      'Descricao' => string 'RETORNO' (length=7)
  1 => 
    array (size=3)
      'IdEntregador' => string '20' (length=2)
      'QtdeRetorno' => string '1' (length=1)
      'Descricao' => string 'RETORNO' (length=7)

The $test variable returns null C: wamp64 www Solus abstract.php:381:null

Honestly, I don’t know what I could be doing wrong, I have four other structures that are working perfectly, I’ve already checked all the information and I don’t know what’s wrong. I had another situation similar to this, but I solved in 2 min, the problem was in the foreach structure, which is not the case now.

  • In line 377 the correct is $sqlEntregasRetornos_res->fetchAll(PDO::FETCH_ASSOC)

  • It was missing a '-' before the '>'

  • I think that is not the problem, because if you notice, I inserted the '-> ' in this case it would return an 'Error' instead of the existing value in the variable.

1 answer

0


In your dump is clearly shown what the problem is. Is that lstEntregasRet is getting ALL query lines.

You have to make a foreach of lstEntregasRet and catch each subarray,

foreach ($lstEntregasRet as $linha)
{
    $IdEntregador = $linha['IdEntregador'];
    ...
}
  • I’ll do it! In my ignorance I thought FETCH_ASSOC behaved like a FETCH_OBJ displaying information from within the variable. Thank you so much for your attention.

Browser other questions tagged

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