title: PHP Error: Trying to get property of non-object
body: <p>I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem...
  I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database.
  The problem comes up when I am trying to access the data from the controller, in my view.
  Error I am getting for each echo in my loop in 'view_thread_view' is</p>
  
  <blockquote>
    <p>A PHP Error was encountered</p>
    
    <p>Severity: Notice</p>
    
    <p>Message: Trying to get property of non-object</p>
    
    <p>Filename: views/view_thread_view.php</p>
  </blockquote>
  
  <p>Any solutions would be greatly appreciated.</p>
  
  <p>Here is my code:</p>
  
  <p>Controller thread</p>
  
  <pre><code>function view_thread()
      {
          $quest = $this-&gt;uri-&gt;segment(3);
          echo $quest;// for checking if uri segment is passed
  
          if($query = $this-&gt;data_model-&gt;get_thread($quest))
          {
              $data['records'] = $query;      
          }
  
          $this-&gt;load-&gt;view('view_thread_view', $data);
  </code></pre>
  
  <p>Model data_model</p>
  
  <pre><code>public function get_thread($quest)
     {
      if($q = $this-&gt;db-&gt;get_where('questions' , 'qid' , $quest));
      {
        return $q;
      }
     }
  </code></pre>
  
  <p>View view_thread_view</p>
  
  <pre><code>&lt;div title="question_view"&gt;
      &lt;h1&gt;Question View&lt;h1&gt;
          &lt;?php foreach($records as $data) : ?&gt;
          &lt;div&gt;
              &lt;h2&gt;Title&lt;/h2&gt;
              &lt;?php 
              echo $data-&gt;title;
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;h2&gt;Question&lt;/h2&gt;
              &lt;?php
              echo $data-&gt;contents
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;h2&gt;Tags&lt;/h2&gt;
              &lt;?php
              echo $data-&gt;tags
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              Thread owner
              &lt;?php
              echo $records-&gt;uname
              ?&gt;
          &lt;/div&gt;
      &lt;?php endforeach; ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>EDIT: QUESTION ANSWERED </p>
  
  <p>Thanks to Girish Jangid fixed the problem this is the working code:</p>
  
  <p>Controller</p>
  
  <pre><code>function view_thread()
      {
          $quest = $this-&gt;uri-&gt;segment(3);
          echo $quest;// for checking if uri segment is passed
          //$data = array();
          if($query = $this-&gt;data_model-&gt;get_thread($quest))
          {
              $data['records'] = $query;      
          }
  
          $this-&gt;load-&gt;view('view_thread_view', $data);
      }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>public function get_thread($quest)
     {
      if($q = $this-&gt;db-&gt;get_where('questions' , array('qid' =&gt; $quest)));
      {
        return $q;
      }
  
     }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;div title="question_view"&gt;
      &lt;h1&gt;Question View&lt;h1&gt;
  
          &lt;?php foreach($records-&gt;result() as $data) : ?&gt;
          &lt;div&gt;
              &lt;h2&gt;Title&lt;/h2&gt;
              &lt;?php 
              echo $data-&gt;title;
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;h2&gt;Question&lt;/h2&gt;
              &lt;?php
              echo $data-&gt;contents
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;h2&gt;Tags&lt;/h2&gt;
              &lt;?php
              echo $data-&gt;tags
              ?&gt;
          &lt;/div&gt;
          &lt;div&gt;
              Thread owner
              &lt;?php
              echo $data-&gt;uname
              ?&gt;
          &lt;/div&gt;
      &lt;?php endforeach; ?&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>