Problem trying to list error -> "Trying to get Property of non-object in"

Asked

Viewed 1,212 times

0

I have the following error: Trying to get Property of non-object in.

Found in this code snippet.

<tr class="success">
      <td> <?=$reg->getServico ?></td>     ******//Erro se encontra nesta linha//************************
      <td> <?=$reg->getValor ?> </td>
      </tr>

I don’t know what it is, if anyone can help me, follow the code below:

class o dao service.

 <?php 
 require_once('./until/bd.class.php');
 $objBd = new bd();
 $con = $objBd->conecta_mysql();

class ServicoDAO{

     public function listar_servico( ){
     global $con;

     $servico_vo = new ServicoVO();
     $query = ("SELECT * FROM servico ");
     $stmt = $con->query($query);

     while ($rs= $stmt->fetch_array()) {
          $servico_vo->setIdServico($rs['id_servico']) ;
          $servico_vo->setServico($rs['nome_servico']) ;
          $servico_vo->setValor($rs['valor']);
          return $rs;
      }      
    }
}

Page that displays values

    <body>

<div class="container">

       <div class="page-header">
         <h1>Listar Serviço</h1> 
       </div>


       <table class="table table-striped table-bordered table-hover table-condensed  ">

         <thead>
           <th>Produto</th>          
           <th>Fabricante</th> 
           <th>Preço</th> 
         </thead>

         <tbody>
          <?php

           $CPDAO = new ServicoDAO();
           $query = $CPDAO->listar_servico();

           foreach($query as $reg):


          ?>
          <tr class="success">
          <td> <?=$reg->getServico ?></td>     ******//Erro se encontra nesta linha//************************
          <td> <?=$reg->getValor ?> </td>
          </tr>


        </tbody>
      <?php 

      endforeach;
      ?>
      </table>


</body>
  • Can you put the complete error? On which line is?

  • Cara has already been solved.... The problem already in the description is the error is found in the line described as "/Error is found in this line//".

  • @junio seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

  • Yes sorry , I thought just write "solved" finished the post , but I marked the answer below as accepted already , not knowing that was the solution.

  • Sorry for the inconvenience , but I’m new to the forum. I already edited the post.

1 answer

4


Your listar_servico() method does not return an object, but an array. But there is one detail: when you give a $rs Return there inside the while, you are at the same time interrupting the loop. This way while will stop and set the return $rs to your method as soon as you run the first record.

Do it this way:

    $registros = array();
    while ($rs= $stmt->fetch_array()) {
              $servico_vo->setIdServico($rs['id_servico']) ;
              $servico_vo->setServico($rs['nome_servico']) ;
              $servico_vo->setValor($rs['valor']);
              $registros[] = $rs;
          }

   return (object) $registros; // Isso irá converter seu array $registros para um objeto
  • Thank you so much for the reply, but you are returning me the same error Trying to get Property of non-object in.

  • Give a var_dump($query) there before the foreach, to see how the structure is getting

  • This returns all the values of the array. Object(stdClass)#6 (10) { [0]=> array(6) { [0]=> string(2) "21" ["id_servico"]=> string(2) "21" [1]=> string(2) "PC" ["service_name"]=> string(2) "PC" [2]=> string(7) "1500.00" ["value"]=> string(7) "1500.00" } [1]=> array(6) { [0]=> string(2) "22" ["id_servico"]=> string(2) "22" [1]=> string(2) "PC" ["service_name"]=> string(2) "PC" [2]=> string(7) "2000.00" ["value"]=> string(7) "2000.00""

  • getServico and getValor attributes are not defined. I believe you should use <?=$reg->service_name ? > and <?=$reg->value ?>

  • You’re making the same mistake again.

  • 1

    e <?=$reg["service_name"] ? > and <?=$reg["value"] ? > ? I think the main array has been converted to object, but its elements have not. If this happened, they will only be accessible this way. Try there

  • Wallace Magalhães , very obg. Everything went right here.

Show 2 more comments

Browser other questions tagged

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