AsyncMysqlConnection::multiQuery

Begin running a query with multiple statements.

Description

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

Begin running a query with multiple statements.

AsyncMysqlConnection::multiQuery() is similar to AsyncMysqlConnection::query(), except that you can pass an array of string queries to run one after the other. Then when you await or join on the returned Awaitable, you will get a Vector of AsyncMysqlQueryResult, one result for each query.

We strongly recommend using multiple calls to queryf() instead as it escapes parameters; multiple queries can be executed simultaneously by combining queryf() with HH\Asio\v().

Parameters

  • Traversable<string> $query
  • 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

AsyncMysqlConnection::multiQuery is similar to AsyncMysqlConnection::query, except that you can pass an array of queries to run one after the other. Then when you await on the call, you will get a Vector of AsyncMysqlQueryResult, one result for each query.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\Conn\MultiQuery;

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_multi_query(): Awaitable<int> {
  // In our test database, the third query will return an empty result since
  // we do not have a user ID of 3.
  $queries = Vector {
    'SELECT name FROM test_table WHERE userID = 1',
    'SELECT age, email FROM test_table WHERE userID = 2',
    'SELECT name FROM test_table WHERE userID = 3',
  };
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  $results = await $conn->multiQuery($queries);
  $conn->close();
  $x = 0;
  foreach ($results as $result) {
    $x += $result->numRows();
  }
  return $x;
}

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

run();
Output
int(2)