AsyncMysqlRowIterator::rewind
Reset the iterator to the first field (column)
public function rewind(): void;
Returns
void
Examples
The following example shows how to use AsyncMysqlRowIterator::rewind
to get back to the beginning of the iterator as necessary.
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(darray[]);
$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();
}
// If we didn't find something to return
$it->rewind();
while ($it->valid()) {
// current() will give you a string value of the field in the row
if ($it->key() > 0 && \is_string($it->current())) {
return 1;
}
$it->next();
}
}
return -1;
} else {
return -1;
}
}
<<__EntryPoint>>
async function run(): Awaitable<void> {
$r = await iterate();
\var_dump($r);
}
```.hhvm.expectf
int(%d)
```.example.hhvm.out
int(42)
```.skipif
await \Hack\UserDocumentation\API\Examples\AsyncMysql\skipif_async();