Error while passing variable

Asked

Viewed 45 times

0

I’m trying to pass a variable, but it doesn’t appear on the screen.

The mistake...

Notice: Undefined index: operador_mapa in C: xampp htdocs Sistema Views form_mapa_continua.php on line 58

Here’s where I call the job:

$nome = $busca_mapas->busca_mapa($id);

Here is the function:

public function busca_mapa($id){
    $conexao = Database::getConnection();

    $busca = "SELECT * from mapa WHERE cod_mapa = $id;";
    $mapa = $conexao->query($busca);
    $retorno = $mapa->fetchAll(PDO::FETCH_ASSOC);

    return $retorno;

}    

Here is the html code where I print the variable (and that’s where, too, the error appears):

<div class="disabled field">
    <label>Nome do operador</label>
       <input value="<?= $nome['operador_mapa'];?>" placeholder="<?=$nome['operador_mapa']?>">
</div>

The bank:

inserir a descrição da imagem aqui

I can assure you that there is data in the bank.

  • Can you var_dump the $name and post here the result please?

1 answer

3


You used the fetchAll, that returns a array results. As you are searching for the id, you will always have only one result, causing $nome resemble:

$nome = [
    0 => [
        'setor_mapa' => ...,
        'operador_mapa' => ...,
        ...
    ]
];

Thus, to display a value, it would be necessary $nome[0]['operador_mapa']; or you use the method fetch, which already returns the result.

  • Wow, you solved it!!! Thank you! :)

Browser other questions tagged

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