return fetch_object ()

0

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

Thank you 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>
    
asked by anonymous 06.04.2016 / 22:08

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>
    
07.04.2016 / 14:14
0

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

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

return $lista;

Your call is:

<?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 } ?>
    
06.04.2016 / 22:22
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;    
}
    
06.04.2016 / 23:00