Home PHP MySQLi mysqli_reap_async_query() PHP mysqli_reap_async_query() Function 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.
$result = mysqli_reap_async_query($mysql); Call What 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 mysqli_reap_async_query(mysqli $mysql): mysqli_result|bool Parameters Parameter Description mysql Required. Procedural style only: a mysqli link from mysqli_connect() or mysqli_init().
Return Value Return Value Description mysqli_result A buffered result object for successful queries that produce a result set. true The async query succeeded and did not produce a result set. false The async query failed, or reaping was not possible.
PHP Examples 1. Reap After Poll Reports Ready 2. SELECT Result vs Non-SELECT Success 3. Object Style Reap Call Example 1 — Reap After Poll Reports Ready Demo script that shows the poll → reap flow with fixed sample messages (no live server).
<?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 A real app waits with mysqli_poll() until a link is readable. Then mysqli_reap_async_query($link) gathers that async query. This demo prints a fixed sample row name instead of talking to MySQL. 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
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;
}
} select: result object
ok: true (no result set)
fail: false How It Works SELECT-style async work yields a result object you can fetch from. Successful INSERT/UPDATE/DELETE style work yields true. Failure yields false — check mysqli_error() / sqlstate() on the link. Always free a real mysqli_result when you are done. Frequently Asked Questions What does mysqli_reap_async_query() do? It collects the result of a query that was started with the MYSQLI_ASYNC flag after mysqli_poll() reports the connection is ready.
Does it work with every MySQLi build? No. Async query support needs mysqlnd. Without mysqlnd, this API is not available.
What does it return? A mysqli_result for result-producing queries, true for other successful queries, or false on failure.
How does it relate to mysqli_poll()? poll() waits until one or more async links are ready. reap_async_query() then reads each finished link.
Should beginners use async MySQLi? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion