title: PHP Mysqli Prepared Statement In a Function To Handle When Records Don't Exist
body: <p>I have found lot of places on SO where pieces of what I want to do have been mentioned but I am having a hard time to get it to work when I combine. Here's what I want to do
  I have a function that queries my database and shows the results. I want to check if no records exist to display the message "No records found".</p>
  
  <pre><code>public function testFun(){
  
          $stmt = $this-&gt;dbconn-&gt;prepare('SELECT FirstName FROM users u JOIN usersandroles ur ON ur.UserId = u.id');
          $stmt-&gt;execute();
          $result = $stmt-&gt;get_result();
          $stmt-&gt;store_result();
          $rowCount = $stmt-&gt;num_rows;
          $stmt-&gt;free_result();
          $stmt-&gt;close();                
          if($rowCount === 0){
              return FALSE;
          }else{
              return $result;
          }
      }
  </code></pre>
  
  <p>My PHP page</p>
  
  <pre><code>$db = generaldb::getInstance();
  $user = new User($db);
  $result = $user-&gt;testFun();
  if($result === FALSE){
      echo 'No rows';
  }  else {
      while($rows = $result-&gt;fetch_assoc()){
          echo $rows['FirstName'],'&lt;/br&gt;';
      }
  }
  </code></pre>
  
  <p>I keep getting "No rows"
   even though there are 5 records. Please help.</p>
  
  <p>Thanks.</p>