Why store result set when determing the number of rows returned in PHP prepared statement for select query?
<p>So after struggling for quiet some time I came across the solution that I need to use store_result() function to transfer the rows returned(from select query) to I don't know where, but it seems unnecessary and even costly to first transfer the results and then checking number of rows. </p>

<p>eg..</p>

<pre><code>$sql = "SELECT * FROM login_info WHERE user_id=? AND password=?";

        //Prepare statement
        $stmt = $conn-&gt;prepare($sql);

        //bind parameters to prepared statement
        $stmt-&gt;bind_param("ss",$userId,$password);

        //execute query
        $stmt-&gt;execute();

        //fetch number of rows returned
        echo $count = $stmt-&gt;num_rows;
</code></pre>

<p>$count value is 0. But after adding $stmt->store_result(); to the above code it works.</p>

<pre><code>$sql = "SELECT * FROM login_info WHERE user_id=? AND password=?";

        //Prepare statement
        $stmt = $conn-&gt;prepare($sql);

        //bind parameters to prepared statement
        $stmt-&gt;bind_param("ss",$userId,$password);

        //execute query
        $stmt-&gt;execute();

        //store result  
        $stmt-&gt;store_result();

        //fetch number of rows returned
        echo $count = $stmt-&gt;num_rows;
</code></pre>

<p>Now code prints the number of rows as 1 in count variable.</p>