AsyncMysqlRow::getFieldAsString
Get a certain field (column) value as a string
public function getFieldAsString(
mixed $field,
): string;
Parameters
mixed $field
- the field index (int
) or field name (string
).
Returns
string
- Thestring
value of the field (column).
Examples
The following example shows how to get a field value as an string
value via AsyncMysqlRow::getFieldAsString
. Assume this was the SQL used to create the example table:
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)
);
In this case, we are actually getting a field that is a string
(or VARCHAR
).
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(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);
return $row->getFieldAsString('name'); // string
}
return "";
} else {
return "";
}
}
<<__EntryPoint>>
async function run(): Awaitable<void> {
$r = await simple_query();
\var_dump($r);
}
```.hhvm.expectf
string(%d) "%s"
```.example.hhvm.out
string(3) "Jan"
```.skipif
await \Hack\UserDocumentation\API\Examples\AsyncMysql\skipif_async();