title: Illegal string offset error in codeigniter view
body: <p>im doing a PHP project using codeigniter 2.1.2 , using mySql database on WAMP, i have stumbled upon the following problem:</p>
  
  <p>Here is my model</p>
  
  <pre><code>function loadUserData() {
      $sql = "SELECT username FROM user WHERE username = ?";
      $query = $this -&gt; db -&gt; query($sql, $this-&gt;session-&gt;userdata('username'));
  
      if ($query -&gt; num_rows() &gt; 0) {
          foreach ($query-&gt;result() as $row) {
              $data = $row;
          }
          return $data;
      }
  
  }
  </code></pre>
  
  <p>The controller</p>
  
  <pre><code>function profile_access() {
      if ($this -&gt; session -&gt; userdata('is_logged_in')) {
          $this-&gt;load-&gt; model('profile_model');
          $uData['rows']=$this-&gt;profile_model-&gt;loadUserData();
          $this-&gt;load-&gt;view('profile_view',$uData);
  
      } else {
          echo "&lt;script type=\"text/javascript\"&gt; alert(\"You need to provide your credentials first.\");&lt;/script&gt;";
          redirect('login_controller/index');
      }
  }
  </code></pre>
  
  <p>and the view</p>
  
  <pre><code>&lt;?php
        foreach($rows as $r){
                echo print_r($r);
                echo $r[username]-&gt;username; //line no. 81 in my code
              }
  ?&gt;
  </code></pre>
  
  <p>the <code>print_r($r)</code> from view gives me the following errors:</p>
  
  <pre><code>alphaomega1
  A PHP Error was encountered
  Severity: Notice
  Message: Use of undefined constant username - assumed 'username'
  Filename: views/profile_view.php
  Line Number: 81
  
  
  A PHP Error was encountered
  Severity: Warning
  Message: Illegal string offset 'username'
  Filename: views/profile_view.php
  Line Number: 81
  
  
  A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object
  Filename: views/profile_view.php
  Line Number: 81
  </code></pre>
  
  <p>I expect the above code to return only "alphaomega" which is the username in the database. it is however displayed, but i dont know why i'm getting those errors. would appreciate some help with this. thanks.</p>