Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Getting total rows in a query result...</h2> <p>You could just iterate the result and count them. You don't say what language or client library you are using, but the API does provide a <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-num-rows.html" rel="noreferrer">mysql_num_rows</a> function which can tell you the number of rows in a result.</p> <p>This is exposed in PHP, for example, as the <a href="http://php.net/manual/en/mysqli-result.num-rows.php" rel="noreferrer">mysqli_num_rows</a> function. As you've edited the question to mention you're using PHP, here's a simple example using mysqli functions:</p> <pre><code>$link = mysqli_connect("localhost", "user", "password", "database"); $result = mysqli_query($link, "SELECT * FROM table1"); $num_rows = mysqli_num_rows($result); echo "$num_rows Rows\n"; </code></pre> <h2>Getting a count of rows matching some criteria...</h2> <p>Just use COUNT(*) - see <a href="http://dev.mysql.com/doc/refman/5.0/en/counting-rows.html" rel="noreferrer">Counting Rows</a> in the MySQL manual. For example:</p> <pre><code>SELECT COUNT(*) FROM foo WHERE bar= 'value'; </code></pre> <h2>Get total rows when LIMIT is used...</h2> <p>If you'd used a LIMIT clause but want to know how many rows you'd get without it, use <a href="http://dev.mysql.com/doc/refman/5.0/en/select.html#id959825" rel="noreferrer">SQL_CALC_FOUND_ROWS</a> in your query, followed by SELECT <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows" rel="noreferrer">FOUND_ROWS();</a></p> <pre><code>SELECT SQL_CALC_FOUND_ROWS * FROM foo WHERE bar="value" LIMIT 10; SELECT FOUND_ROWS(); </code></pre> <p>For very large tables, this isn't going to be particularly efficient, and you're better off running a simpler query to obtain a count and caching it before running your queries to get pages of data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload