0

I just want to back my class properties so that I can use them. In my project I want to fetch all result of mysql table data and return it. Well it works well, but alter returning data the error message has come which is given below:

   Trying to get property of non-object in E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26

My code ::

nonClass.php

 <?php
      $doc = new Dortor();  // object  

      $doc->result("SELECT * FROM doctor");   // function call

      foreach( $doc as $doctor ) {
         echo $doctor->doc_name;     // error msg: Trying to get property of non-object in
                                   //E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
      }
  ?>

dortor.php

  <?php
     class Dortor extends DatabaseObject {
       public $table_name = "doctor";
       public $id;
       public $doc_name;
       public $doc_pass;
       public $doc_img;     // image directory

       public function result($sql="") { 
         $result = $database->query($sql);  // run query from mysql database

         $result_array = array();   
         while( $row = $database->fetch_array($result)  ) {  // this while loop return actual result
             $result_array[] = $this->get_table_value($row);
         }
         return $result_array;
        }
     }
  ?>

databaseObject.php

  <?php
       class DatabaseObject {
           public function get_table_value($record) {
              $className = get_called_class();  // get the class name
              $object = new $className;       // object call

              $arrayValue = array();  
              $i=0;   // inital array position
              foreach( $object as $key => $value )  {
                  $arrayValue[$i] = $key;  // get the class's object attributes
                  $i++;
              } 

              $i = 1;
             foreach ($record as $key => $value) {   // fetch the database result
                 if(array_key_exists($key, $arrayValue) ) {  // check if database's column name is exist on 'arrayValue' array,
                    $object->$arrayValue[$i] = $value;  //  if exist then, store value in 'Doctor' class's object attributes
                    $i++;
                 }
              } 
             return $object;
          }
       }
  ?>
tereško
  • 58,060
  • 25
  • 98
  • 150
user2013
  • 538
  • 5
  • 14

2 Answers2

0

Your error is probably here:

$doc = new Dortor();  // object  
$docs = $doc->result("SELECT * FROM doctor");   // you forgot to assign the result
foreach ($docs as $doctor) { // was looping over $doc here, instead of the result
   echo $doctor->doc_name;
}
Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

It would also make sense to check the correct variable type before the foreach loop starts:

if ( is_array( $doc)  )
{
 foreach( $doc as $doctor ) 
  {
    // foreach code
  }
}
else
{
  echo '$doc is no valid object';
  var_dump($doc);
}

So you can find out, that's the problem.

Stefan Brendle
  • 1,545
  • 6
  • 20
  • 39