title: PHP Error "Trying to get property of non-object"
body: <p>OK, I have been having trouble for the week with this error, and what I've found i think i understand but when i change it it doesn't work. So if someone could help me understand a little better and tell me why this is not working. </p>
  
  <p>The RunDown-  I am trying to Display a list of the different Product families on my table.</p>
  
  <p>Ok. So here is my Class</p>
  
  <pre><code> &lt;?php
  class mclean {
  
   public $Product_id, $Catalog_Number, $Model_Code, $Caterogry, $Product_Family, $Product_Sub_Family, $Product_Name, $img;
  
  function __construct($Caterogry){
  
      // $db is defined in config.inc.php
      global $db;
  
      // sanitize input
      $Caterogry = $db-&gt;real_escape_string($Caterogry);
  
      // sql query
      $sql = "SELECT * FROM Mclean_Products_info      
              WHERE Caterogry = '". $Caterogry . "'";
  
      // if there is a result...
      if($result = $db-&gt;query($sql)){
  
          // $row contains an object 
          $row = $result-&gt;fetch_object();
  
          // Set class attributes
          $this-&gt;Product_id = htmlentities($row-&gt;Product_id);
          $this-&gt;Catalog_Number = htmlentities($row-&gt;Catalog_Number);
          $this-&gt;Model_Code = htmlentities($row-&gt;Model_Code);
          $this-&gt;Caterogry = htmlentities($row-&gt;Caterogry);
          $this-&gt;Product_Family = htmlentities($row-&gt;Product_Family);
          $this-&gt;Product_Sub_Family = htmlentities($row-&gt;Product_Sub_Family);
          $this-&gt;Product_Name = htmlentities($row-&gt;Product_Name);
          $this-&gt;img = htmlentities($row-&gt;AllProductImages);
  
          // Free result set 
          $result-&gt;close();
  
          // if there are no results...   
      }else{
          // there is probably an error
          printf("Query failed: %s\n", $db-&gt;error);
          exit();
      }
  }
  
  
  // Returns an array of all categories
  public static function get_categories(){
  
      //$db is defined in config.inc.php
      global $db;
      global $site_brand_ids;
  
      //sql query
      $sql = "SELECT DISTINCT product_family_id FROM mclean
              LEFT JOIN product_families USING(product_family_id)
              LEFT JOIN categories USING(category_id)
              WHERE category_id = '91'";
  
  
      //if there is a result
      $categories = array();
      if ($result = $db-&gt;query($sql)) {
          while ($row = $result-&gt;fetch_object()){
              $categories[] = new mclean($row-&gt;Caterogry);
          }
  
          //Free result set
          $result-&gt;close();
  
      }else{ // if there are no results
  
          // There is a possible error
          printf("Query failws: %s\n", $db-&gt;error);
          exit();
      }
  
      //return array
      return $categories;
  } //End of get_categories
  }
  
  ?&gt;
  </code></pre>
  
  <p>Now here is how i am calling it for the Display.</p>
  
  <pre><code>&lt;?php
              $categories = array('Accessories', 'Thermal Management', 'Busbar Systems');
              $menu = array();
                      foreach($categories as $Caterogry){
                          $Caterogry = new mclean($Caterogry);
                          $menu[$Caterogry-&gt;Product_Family] = $Caterogry;
                      }
                      foreach($menu as $Caterogry =&gt; $Caterogry){
                          $Product_Family = $Caterogry-&gt;Product_Family;
                          $Product_Sub_Family = ($Caterogry-&gt;Product_Sub_Family);
                          echo '&lt;div class="large-4 columns"&gt;&lt;div class="product_category"&gt;&lt;h4&gt;' . $Product_Sub_Family . '&lt;/h4&gt;';
                          $Product_Sub_Family= toAscii($Product_Sub_Family);
                          $category_link = $Product_Family;
                          $category_link = strtolower($category_link);
                      //  $category_link = str_replace("-", "_", $category_link);
                          echo '&lt;a href="/' . $category_link . '"&gt;&lt;p&gt;' . $Product_Sub_Family . '&lt;span&gt;Learn More&amp;ensp;&amp;raquo;&lt;/span&gt;&lt;/p&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;';
                       //   echo '&lt;a href="/' . '_products/' . '"&gt;&lt;p&gt;' . $category_description . '&lt;span&gt;Learn More&amp;ensp;&amp;raquo;&lt;/span&gt;&lt;/p&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;';
                      }
              ?&gt;
  </code></pre>
  
  <p>Then I get This error on the page:</p>
  
  <p>An error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 29: 
  Trying to get property of non-object 
  An error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 30: 
  Trying to get property of non-object </p>
  
  <p>Lines 29: and 30 is this:</p>
  
  <pre><code> $Product_Family = $Caterogry-&gt;Product_Family;
   $Product_Sub_Family = ($Caterogry-&gt;Product_Sub_Family);
  </code></pre>
  
  <p>Any Help or guidance will be appreciated , I have also tried </p>
  
  <pre><code>var_dump($Product_Family);
  var_dump($Caterogry-&gt;Product_Family);
  exit();
  </code></pre>
  
  <p>and for some reason it brings back NULL? But why? </p>