title: Trying to get property of non-object MySQLi result
body: <p>Got a bit of PHP code I'm struggling with - had a search around Google etc. and tried everything mentioned, but for some reason I'm having trouble solving it.</p>
  
  <p>The problem is:</p>
  
  <p>I have some code that is querying a database for the presence of a particular user.</p>
  
  <p>The code (it's a method inside a class)</p>
  
  <pre><code>&lt;?php
  global $mysqli;
  // Query specified database for value
  $q = 'SELECT id FROM ' . $database . ' WHERE username = \'' . $username . '\'';
  $r = $mysqli-&gt;query($q);
  var_dump($r);
  if ($r-&gt;num_rows) {
      // If row found username exists so return false
      return false;
  }
  ...
  ?&gt;
  </code></pre>
  
  <p>I've var dumped the result of the query ($r) and got this:</p>
  
  <pre><code>object(mysqli_result)#4 (5) { ["current_field"]=&gt; int(0) ["field_count"]=&gt; int(1) ["lengths"]=&gt; NULL ["num_rows"]=&gt; int(1) ["type"]=&gt; int(0) }
  </code></pre>
  
  <p>This is correct, there should only be 1 row as above.</p>
  
  <p>I do get this error linking to the line saying <code>if ($r-&gt;num_rows) {</code></p>
  
  <p>Notice: Trying to get property of non-object in FILE on line LINE</p>
  
  <p>but I don't know why since the object is valid (as above) and it should be working fine. From what I can tell it seems to be going through alright, I'm just wondering why there's an error. I'm sure it's something simple but I'd appreciate any help.</p>