AsyncMysqlRow::getIterator

Description

public function getIterator(): AsyncMysqlRowIterator

Get the iterator over the fields in the current row.

Return Values

Examples

The following example shows you how to get an iterator of an AsyncMysqlRow via getIterator(). Getting an iterator of an AsyncMysqlRow gives you an AsyncMysqlRowIterator, where each key of that iterator is an int representing the key to the field of the AsyncMysqlRow, and each value from current() is the value of the field of that AsyncMysqlRow.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\Row\GetIterator;

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 iterate(): Awaitable<int> {
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  $result = await $conn->query(
    'SELECT * FROM test_table WHERE userID < 50'
  );
  $conn->close();
  // A call to $result->rowBlocks() actually pops the first element of the
  // row block Vector. So the call actually mutates the Vector.
  $row_blocks = $result->rowBlocks();
  if (!$row_blocks->isEmpty()) {
    // An AsyncMysqlRowBlock
    $row_block = $row_blocks[0];
    if (!$row_block->isEmpty()) {
      // An AsyncMysqlRow
      $row = $row_block->getRow(0);
      // An AsyncMysqlRowIterator
      $it = $row->getIterator();
      while ($it->valid()) {
        // current() will give you a string value of the field in the row
        if ($it->key() > 0 && is_numeric($it->current())) {
          return intval($it->current());
        }
        $it->next();
      }
    }
    return -1;
  } else {
    return -1;
  }
}

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

run();
Output
int(41)