Return fetch_object()

Asked

Viewed 86 times

0

My script is a function (query.php) that queries the mysql database and with while I run all object contents ($qr = $query->fetch_object())). If I give a print_r inside I can see all the data but when I try to use the Return to get the data on another page nothing happens.

Thanks in advance.

while($data = $query->fetch_object()){
                 $data->id;
                 $data->modelo;
                 $data->login;
                 $data->senha;
                 $data->cadin;
                 $data->cadin;
                 $data->status;
                 $data->idSessao;
                 $data->dataHora;

                 print_r($data); //esse print contem todo conteudo
            }   
                print_r($data); //esse print nao contem nada
                $this->data = &$data;
                return true;
            }

In the other file where I get the data have this line:

$lista = $modelObj->data;
//aqui não tenho valor algum
Trecho que usa os dados de return

<?
        $modelObj->Consulta();
        $lista = $modelObj->var;
        //esse print nao tem valor  
        print_r($lista);

        ?>
            <div class="col200"><?=$lista['id'];?></div>
            <div class="col100 center"><?=$lista['modelo'];?></div>
            <div class="col100 center"><?=$lista['login'];?></div>
            <div class="col100 center"><?=$lista['senha'];?></div>
            <div class="col100 center"><?=$lista['cadin'];?></div>
            <div class="col100 center"><?if($lista['status'] == 1){echo "Ativo";}else{echo "Desativado";};?></div>
            <div class="col100 center"><?=$lista['idSessao'];?></div>
            <div class="col100 center"><?=$lista['dataHora']?></div>
            <div class="col100 center"></div>
            <div class="clear"></div>
  • vc should make the array and not true and another only the last record is played in date.

  • Put the code where you call this function, it’s kind of weird ...

  • Take the piece of resolution and create an answer with it :)

3 answers

1

=================Resolution=====================================

while($data = $query->fetch_object()){

                $lista[$data->id]['id'] = $data->id;
                $lista[$data->id]['modelo'] = $data->modelo;
                $lista[$data->id]['login']  = $data->login;
                $lista[$data->id]['senha']  = $data->senha;
                $lista[$data->id]['cadin']  = $data->cadin;
                $lista[$data->id]['status'] = $data->status;
                $lista[$data->id]['idSessao']=$data->idSessao;
                $lista[$data->id]['dataHora']=$data->dataHora; 
            }   
             $this->data = &$lista;
              return true;

        $modelObj->consulta();
        $lista = $modelObj->data;
        //print_r($lista);
        ?>

            <div class="col200"><?=$lista['id'];?></div>
            <div class="col100 center"><?=$lista['modelo'];?></div>
            <div class="col100 center"><?=$lista['login'];?></div>

0

I would make the consultar() return an array of objects instead of filling an object property. changes would be:

$lista = [];
while($data = $query->fetch_object()){
    $lista[] = $data;
}

return $lista;

Your call gets:

<?php
   $lista = $modelObj->Consulta();
   foreach($lista as $item){
?>
<div class="col200"><?= $item->id; ?></div>
<div class="col100 center"><?= $item->modelo; ?></div>
<div class="col100 center"><?= $item->login'; ?></div>
//demais códigos ...
<?php } ?>

0

while($data = $query->fetch_object()){
             $data->id;
             $data->modelo;
             $data->login;
             $data->senha;
             $data->cadin;
             $data->cadin;
             $data->status;
             $data->idSessao;
             $data->dataHora;
             $list[] = $data;  
        }

foreach ($list as $row){
    echo $row->id;    
}
  • My friends thank you for your attention, I commented on an amateur error, I apologize. I used the same name of the variable and different times but as it is obvious to imagine it made the value was overwritten so when I sent the Return did not appear anything.

Browser other questions tagged

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