AsyncMysqlConnection::query

Begin running an unsafe query on the MySQL database client.

Description

public function query(
  string $query,
  int $timeout_micros = -1,
): Awaitable<AsyncMysqlQueryResult>

Begin running an unsafe query on the MySQL database client.

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.

Return Values

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.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\Conn\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(array());
  $conn = await connect($pool);
  $result = await $conn->query('SELECT name FROM test_table WHERE userID = 1');
  $conn->close();
  return $result->numRows();
}

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

run();
Output
int(1)