Return the number of rows in statements result set (original) (raw)
mysqli_stmt::$num_rows
mysqli_stmt::num_rows
mysqli_stmt_num_rows
(PHP 5, PHP 7)
mysqli_stmt::$num_rows -- mysqli_stmt::num_rows -- mysqli_stmt_num_rows — Return the number of rows in statements result set
Description
Object oriented style
intmysqli_stmt->num_rows;
public mysqli_stmt::num_rows ( void ) : int
mysqli_stmt_num_rows ( mysqli_stmt $stmt
) : int
If you use mysqli_stmt_store_result(),mysqli_stmt_num_rows() may be called immediately.
Parameters
stmt
Procedural style only: A statement identifier returned by mysqli_stmt_init().
Return Values
An integer representing the number of rows in result set.
Examples
Example #1 Object oriented style
<?php /* Open a connection */ $mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20"; if ($stmt = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><mi>i</mi><mo>−</mo><mo>></mo><mi>p</mi><mi>r</mi><mi>e</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">mysqli->prepare(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">ys</span><span class="mord mathnormal" style="margin-right:0.01968em;">ql</span><span class="mord mathnormal">i</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">re</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">re</span><span class="mopen">(</span></span></span></span>query)) {/* execute query */ $stmt->execute();/* store result */ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>−</mo><mo>></mo><mi>s</mi><mi>t</mi><mi>o</mi><mi>r</mi><msub><mi>e</mi><mi>r</mi></msub><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>f</mi><mo stretchy="false">(</mo><mi mathvariant="normal">"</mi><mi>N</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>o</mi><mi>f</mi><mi>r</mi><mi>o</mi><mi>w</mi><mi>s</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">stmt->store_result();printf("Number of rows: %d.\n", </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6984em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.02778em;">r</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">es</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mopen">(</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord">"</span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mord mathnormal">u</span><span class="mord mathnormal">mb</span><span class="mord mathnormal">ero</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>stmt->num_rows);/* close statement */ $stmt->close(); }/* close connection */ $mysqli->close(); ?>
Example #2 Procedural style
<?php /* Open a connection */ $link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20"; if ($stmt = mysqli_prepare($link, $query)) {/* execute query */ mysqli_stmt_execute($stmt);/* store result */ mysqli_stmt_store_result($stmt);printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));/* close statement */ mysqli_stmt_close($stmt); }/* close connection */ mysqli_close($link); ?>
The above examples will output:
See Also
- mysqli_stmt_affected_rows() - Returns the total number of rows changed, deleted, or inserted by the last executed statement
- mysqli_prepare() - Prepare an SQL statement for execution
- mysqli_stmt_store_result() - Transfers a result set from a prepared statement