AsyncMysqlQueryResult::numRows

The number of rows in the current result.

Description

public function numRows(): int

The number of rows in the current result.

This is particularly useful for SELECT statements.

This is complementary to numRowsAffected() as they might be the same value, but if this was an INSERT query, for example, then this might be 0, while numRowsAffected() could be non-zero.

See the MySQL's mysql_num_rows() documentation for more information.

Return Values

  • int - The number of rows in the current result as an int.

Examples

This example shows how to determine the number of rows returned from a given query using AsyncMysqlQueryResult::numRows.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\QueryResult\NumRows;

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;

async function connect(\AsyncMysqlConnectionPool $pool):
  Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    CI::$passwd
  );
}
async function simple_query(): Awaitable<int> {
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  $result = await $conn->query('SELECT name FROM test_table WHERE userID < 50');
  $conn->close();
  // How many rows did this query return?
  return $result->numRows();
}

function run(): void {
  $r = \HH\Asio\join(simple_query());
  var_dump($r);
}

run();
Output
int(2)