AsyncMysqlQueryErrorResult::numSuccessfulQueries

Returns the number of successfully executed queries

Description

public function numSuccessfulQueries(): int

If there were any successful queries before receiving the error, this will let you know how many of those there were.

Return Values

  • int - The number of successful queries before the error as an int.

Examples

This example shows how we can get the number of successful queries of a multi-query, even though one of those queries gave us an error (which we caught in the exception). This is done via AsyncMysqlQueryErrorResult::numSuccessfulQueries.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\QueryErrorRes\NumSucc;

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 multi_query_error(): Awaitable<?int> {
  $queries = Vector {
    'SELECT name FROM test_table WHERE userID = 1',
    'SELECT age, email FROM test_table WHERE userID = 2',
    'SELECT bogus FROM bogus WHERE bogus = 1',
  };
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  try {
    $result = await $conn->multiQuery($queries);
  } catch (\AsyncMysqlQueryException $ex) {
    $qr = $ex->getResult();
    var_dump($qr instanceof \AsyncMysqlQueryErrorResult);
    // Constructor to the exception takes AsyncMysqlErrorResult, need to
    // ensure typechecker that we have an AsyncMysqlQueryErrorResult
    invariant($qr instanceof \AsyncMysqlQueryErrorResult, "Bad news if not");
    var_dump($qr->numSuccessfulQueries());
    $conn->close();
    return null;
  }
  $conn->close();
  return $result->numRows();
}

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

run();
Output
bool(true)
int(2)
NULL