AsyncMysqlRowBlock::fieldName

Description

public function fieldName(
  int $field,
): string

Returns the name of the field (column).

Parameters

  • int $field - the field index.

Return Values

  • string - The name of the column as a string.

Examples

The following example shows how to use AsyncMysqlRowBlock::fieldName to get the name of a field at a given index in the row block

This is an example of what could have been used to create the table from where we are getting our field flags

CREATE TABLE test_table (
userID SMALLINT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL,
age SMALLINT NULL,
email VARCHAR(60) NULL,
PRIMARY KEY (userID)
);

So, in the example, given the table created with the SQL above, the string "age" would be returned (0-indexed).

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\RowBlock\FieldName;

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<string> {
  $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->count() > 0) {
    // An AsyncMysqlRowBlock
    $row_block = $row_blocks[0];
    return $row_block->fieldName(2); // a string
  } else {
    return "nothing";
  }
}

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

run();
Output
string(3) "age"