Example 1 — Conceptual Async Poll Loop
Sketch the poll/reap pattern with fixed sample output. This is advanced — skip it until you need concurrency.
<?php
// Advanced sketch (mysqlnd + MYSQLI_ASYNC). Fixed sample output for the tutorial.
$link = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_query($link, "SELECT 'test' AS v", MYSQLI_ASYNC);
$all = [$link];
$processed = 0;
do {
$read = $error = $reject = [];
foreach ($all as $l) {
$read[] = $error[] = $reject[] = $l;
}
if (!mysqli_poll($read, $error, $reject, 1)) {
continue;
}
foreach ($read as $l) {
if ($result = mysqli_reap_async_query($l)) {
$row = mysqli_fetch_row($result);
echo $row[0], PHP_EOL;
mysqli_free_result($result);
}
$processed++;
}
} while ($processed < count($all));
mysqli_close($link); How It Works
- MYSQLI_ASYNC starts the query without waiting for the full result.
- mysqli_poll() blocks briefly until a connection is ready.
- mysqli_reap_async_query() collects the finished result.
- Beginners can ignore this pattern and use normal query() instead.

