Current Location: Home> Latest Articles> What is mysqli_stmt::$num_rows? Basic Usage and Practical Scenarios

What is mysqli_stmt::$num_rows? Basic Usage and Practical Scenarios

gitbox 2025-09-04
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This section is unrelated to the article content and can include PHP configuration or test code</span></span><span>
</span><span><span class="hljs-title function_ invoke__">error_reporting</span></span><span>(E_ALL);
</span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-string">'display_errors'</span></span><span>, </span><span><span class="hljs-number">1</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"PHP environment test: OK<br>"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<hr>
<p><h1>What is mysqli_stmt::</span>$num_rows<span>? Basic Usage and Practical Scenarios</h1></p>
<p><p>When working with PHP to interact with a MySQL database, we usually use <code>mysqli

3. Important Considerations

  • You must call store_result() to correctly retrieve num_rows.
  • num_rows only works for SELECT queries and is ineffective for INSERT, UPDATE, or DELETE statements.
  • If the query result is very large, using store_result() will store all data in memory, so memory usage should be considered.

4. Practical Scenarios

Here are some typical scenarios:

  1. In pagination, first count the total number of matching rows to calculate the total number of pages.
  2. Check whether a query returned any data, such as verifying if a user account exists during login.
  3. Before batch processing data, knowing the number of rows helps display progress or handle data in batches.

In summary, mysqli_stmt::$num_rows is a convenient property for getting the row count of a prepared statement’s result. When combined with store_result(), it makes counting data easy and is suitable for pagination, verification, and data processing scenarios.