AsyncMysqlRowBlock::isNull

Description

public function isNull(
  int $row,
  mixed $field,
): bool

Returns whether a field (column) value is null.

Parameters

  • int $row - the row index.
  • mixed $field - the field index (int) or field name (string).

Return Values

  • bool - true if the column value is null; false otherwise.

Examples

The following example uses AsyncMysqlRowBlock::isNull to check if a field value is null (e.g., if a field was set in SQL to something like age SMALLINT NULL, then that field could be null).

<?hh

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

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 * FROM test_table'
  );
  $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->isNull(1, 'age')) {
      return $row_block->at(1, 'age'); // int
    }
    return -1;
  } else {
    return -1;
  }
}

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

run();
Output
int(26)