note 128017 added to mysqli.poll
| From: | marcin dot wanat at gmail dot com | Date: | Wed, 14 Dec 2022 14:23:55 +0000 |
| Subject: | note 128017 added to mysqli.poll | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219973@lists.php.net to get a copy of this message | ||
It is worth to note that example from this docs will results in waiting for all queries to be
executed before you can reap your results.
5 years ago bishop from php.net has posted solution that allow to fetch results of each query
immediately after executed. For some reason this has never been merged to php manual.
https://bugs.php.net/bug.php?id=70505
The documentation for mysqli_poll suggests iterating through the links after running the poll:
foreach ($links as $link) {
if ($result = $link->reap_async_query()) {
print_r($result->fetch_row());
if (is_object($result))
mysqli_free_result($result);
} else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
$processed++;
}
This is not a good way to go about it when you have queries of different run-times, because
mysqli_reap_async_query() blocks until the query resolves, which is contrary to the nature of the
async poll.
An approach that respects multiple queries of different run times is to run a reaping function over
each of the modified arrays:
$count = mysqli_poll($read, $error, $reject, 1);
if (0 < $count) {
array_walk($read, 'reap');
array_walk($error, 'reap');
array_walk($reject, 'reap');
}
function reap($link) {
$result = mysqli_reap_async_query($link);
if (is_object($result)) {
print_r($result->fetch_assoc()));
mysqli_free_result($result);
} else if (false !== $result) {
print_r($link);
} else {
die(mysqli_error($link));
}
}
This also handles the case where the query is a CRUD and the result of reap_async_query isn't a
result object.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.175 (proxied: 2a02:a31a:a144:b480:c6cb:aba4:3f89:aee)
----
Manual Page -- https://php.net/manual/en/mysqli.poll.php
Edit -- https://main.php.net/note/edit/128017
Del: integrated -- https://main.php.net/note/delete/128017/integrated
Del: useless -- https://main.php.net/note/delete/128017/useless
Del: bad code -- https://main.php.net/note/delete/128017/bad+code
Del: spam -- https://main.php.net/note/delete/128017/spam
Del: non-english -- https://main.php.net/note/delete/128017/non-english
Del: in docs -- https://main.php.net/note/delete/128017/in+docs
Del: other reasons-- https://main.php.net/note/delete/128017
Reject -- https://main.php.net/note/reject/128017
Search -- https://main.php.net/manage/user-notes.php