PHP mysqli_reap_async_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_reap_async_query() collects the result of a query that was started asynchronously on a connection (mysqlnd only).

You typically call it after mysqli_poll() reports that a link is ready. For SELECT-style queries it returns a mysqli_result. For successful non-SELECT queries it returns true. On failure it returns false. Pair it with MYSQLI_ASYNC on query/real_query and with poll().

Syntax

Call mysqli_reap_async_query() with the values you need. Start with this line.

PHP
$result = mysqli_reap_async_query($mysql);
CallWhat it does
mysqli_reap_async_query($link)Returns a mysqli_result for a finished async SELECT, or true for a finished non-SELECT.
$mysqli->reap_async_query()Object-oriented form on the same connection object.
is_object($result) ? $result->fetch_assoc() : 'ok'Treat a result object differently from a plain true success.

Available only with mysqlnd. Use mysqli_poll() to wait until a connection has finished, then reap. Free any mysqli_result you receive. For INSERT/UPDATE/DELETE, affected_rows and insert_id live on the connection, not on a falsey non-object return.

Library declarations
PHP
mysqli_reap_async_query(mysqli $mysql): mysqli_result|bool

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli link from mysqli_connect() or mysqli_init().

Return Value

Return ValueDescription
mysqli_resultA buffered result object for successful queries that produce a result set.
trueThe async query succeeded and did not produce a result set.
falseThe async query failed, or reaping was not possible.

PHP Examples

Example 1 — Reap After Poll Reports Ready

Demo script that shows the poll → reap flow with fixed sample messages (no live server).

PHP
<?php
// Demo: pretend poll() marked $link as ready.
$ready = true;
$demoResult = ['id' => 1, 'name' => 'Ada'];

if ($ready) {
    // In real code: $result = mysqli_reap_async_query($link);
    $result = $demoResult; // stand-in for a fetched row
    echo 'reaped row: ', $result['name'], PHP_EOL;
}

How It Works

  1. A real app waits with mysqli_poll() until a link is readable.
  2. Then mysqli_reap_async_query($link) gathers that async query.
  3. This demo prints a fixed sample row name instead of talking to MySQL.
  4. Sample output when connected to a demo database would look similar after fetch_assoc().

Example 2 — SELECT Result vs Non-SELECT Success

Show how to branch when reap returns an object versus true.

PHP
<?php
function demo_reap($kind) {
    if ($kind === 'select') {
        return (object) ['kind' => 'mysqli_result'];
    }
    if ($kind === 'ok') {
        return true;
    }
    return false;
}

foreach (['select', 'ok', 'fail'] as $kind) {
    $r = demo_reap($kind);
    if (is_object($r)) {
        echo $kind, ': result object', PHP_EOL;
    } elseif ($r === true) {
        echo $kind, ': true (no result set)', PHP_EOL;
    } else {
        echo $kind, ': false', PHP_EOL;
    }
}

How It Works

  1. SELECT-style async work yields a result object you can fetch from.
  2. Successful INSERT/UPDATE/DELETE style work yields true.
  3. Failure yields false — check mysqli_error() / sqlstate() on the link.
  4. Always free a real mysqli_result when you are done.

Example 3 — Object Style Reap Call

Same idea with the OO method name and a fixed success line.

PHP
<?php
class DemoLink {
    public function reap_async_query() {
        return true;
    }
}

$mysqli = new DemoLink();
echo $mysqli->reap_async_query() === true ? 'async done' : 'async failed', PHP_EOL;

How It Works

  1. OO code calls $mysqli->reap_async_query() with no link argument.
  2. Procedural code passes the link as the first argument.
  3. This stand-in returns true like a finished non-SELECT.
  4. Wire it to a real mysqli only after poll() says the link is ready.

Frequently Asked Questions

It collects the result of a query that was started with the MYSQLI_ASYNC flag after mysqli_poll() reports the connection is ready.
No. Async query support needs mysqlnd. Without mysqlnd, this API is not available.
A mysqli_result for result-producing queries, true for other successful queries, or false on failure.
poll() waits until one or more async links are ready. reap_async_query() then reads each finished link.
Usually no. Prefer mysqli_query() or prepared statements. Async is for advanced multi-connection patterns.

Next: mysqli_refresh()

mysqli_refresh() asks the MySQL server to flush caches or refresh certain server state, us.

mysqli_refresh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful