I'm maintaining a project based on legacy code that was written by an external company. We are in the process of fixing a lot of PHP errors in this code base and one of them comes from a call to mysqli::reap_async_query()
:
mysqli::reap_async_query(): Connection not opened, clear or has been closed
This happens only on the first call to this function:
function mysqlQuery($sql, $getLastId = false, $die = true, $alwaysDie = false, $async = false)
{
@$GLOBALS['con']->reap_async_query(); // This is what is triggering the error
if ($async) {
$result = $GLOBALS['con']->query($sql, MYSQLI_ASYNC);
} else {
$result = $GLOBALS['con']->query($sql);
}
if (!$result) {
if ($alwaysDie or ($_ENV['ENV_MODE'] === 'dev' and $die)) {
die('Etwas stimmte mit dem Query nicht: ' . $GLOBALS['con']->error . '<br/>Ganzes Query: ' . $sql);
}
return false;
} else {
$lastId = mysqlLastId();
if ($getLastId == true) {
return $lastId;
} else {
return $result;
}
}
}
Note that $GLOBALS['con']
is an instance of mysqli
.
The code is calling mysqlQuery()
with no specific parameter:
$result = mysqlQuery("SELECT * FROM someTable");
According to the Git history, the call to @$GLOBALS['con']->reap_async_query();
was added to support async SQL queries. But reading the PHP doc, it doesn't seem to be useful here at all since we are not storing its return value.
So my question is: is there a reason for it to be here, does calling it even without reading its return value have any important side effect ? I might just remove this call completely if it is useless.
Also, why is it triggering this error ? I understand that trying to read a result before any query has been executed could trigger an error but the error indicates that the connection is not active, which does not seem to be the case.