AsyncMysqlConnection::query

Begin running an unsafe query on the MySQL database client

public function query(
  string $query,
  int $timeout_micros = -1,
  dict<string> $query_attributes = dict [
],
): Awaitable<AsyncMysqlQueryResult>;

If you have a direct query that requires no placeholders, then you can use this method. It takes a raw string query that will be executed as-is.

You may want to call escapeString() to ensure that any queries out of your direct control are safe.

We strongly recommend using queryf() instead in all cases, which automatically escapes parameters.

Parameters

  • string $query - The query itself.
  • int $timeout_micros = -1 - The maximum time, in microseconds, in which the query must be completed; -1 for default, 0 for no timeout.
  • dict<string> $query_attributes = dict [ ] - Query attributes. Empty by default.

Returns

Examples

The following example shows a basic usage of AsyncMysqlConnection::query. First you get a connection from an AsyncMysqlConnectionPool, then you can make the query.

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(darray[]);
  $conn = await connect($pool);
  $result = await $conn->query('SELECT name FROM test_table WHERE userID = 1');
  $conn->close();
  return $result->numRows();
}

<<__EntryPoint>>
async function run(): Awaitable<void> {
  $r = await simple_query();
  \var_dump($r);
}
```.hhvm.expectf
int(%d)
```.example.hhvm.out
int(1)
```.skipif
await \Hack\UserDocumentation\API\Examples\AsyncMysql\skipif_async();