title: Codeigniter Trying to get property of non-object error in Login Controller
body: <p>I am new in codeignter. i am trying to make login-register with it. while checking <code>if($user -&gt; email)</code> it giving error saying Trying to get property of non-object of this line. how can i get rid of it. i have successfully made register section, but when trying to logged in it giving error. the controller code is here,</p>
  
  <pre><code>public function home() {
  
      $this-&gt;load-&gt;helper('url');
      $this-&gt;load-&gt;library('form_validation');
      $this-&gt;load-&gt;library('session');
  
      $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'required');
      $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required|min_length[5]');
  
      if ($this-&gt;form_validation-&gt;run() == TRUE) {
  
          $username = $_POST['username'];
          $password = md5($_POST['password']);
  
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('users');
  
          $this-&gt;db-&gt;where(array('username' =&gt; $username, 'password' =&gt; $password));
  
          $query = $this-&gt;db-&gt;get();
  
          $user = $query-&gt;row();
  
          if ($user-&gt;email) {
  
              $this-&gt;session-&gt;set_flashdata("Success", "You have successfully logged in");
  
              $_SESSION['user_logged'] = TRUE;
  
              $_SESSION['username'] = $user-&gt;username;
  
              redirect('profile', 'refresh');
          } else {
  
              $this-&gt;session-&gt;set_flashdata("Error", "No such account in database");
          }
      }
      $this-&gt;load-&gt;view('login');
  }
  </code></pre>
  
  <p>Thanks for your help.</p>