2

I'm using the MySQL native driver for PHP to make asynchronous, parallel queries from a PHP application to a MySQL DB.

I have a function that receives an array of query strings and executes them in parallel.

For whatever reason though, in one particular environment where I'm using this function, the following PHP warning is being output to the Apache error_log en masse:

PHP Warning:  mysqli_poll(): Parameter 1 not a mysqli object

My assumption is that this occurs when one or more of the DB connection variables passed in the array for the first argument of the mysqli_poll function is not a valid MySQLi object, but looking at my code, I don't understand how it could ever reach this point to begin with.

Specifically, here's the code in question:

function makeAsyncQueries($queries) {

  $allLinks = [];
  $allLinkThreadIds = [];
  $allResults = [];

  foreach ($queries as $query) {

    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT);
    $result = mysqli_query($link, $query, MYSQLI_ASYNC);

    if ($result !== false) {
      $allLinks[] = $link;
      $allLinkThreadIds[] = mysqli_thread_id($link);
    } else {
      // Attempting to log. This condition is never met.
    }

  }

  $processed = 0;

  while ($processed < count($allLinks)) {

    $links = $errors = $reject = [];

    foreach ($allLinks as $link) {
      $links[] = $errors[] = $reject[] = $link;
    }

    if (mysqli_poll($links, $errors, $reject, 0, 0)) { // This causes the error.

      foreach ($links as $link) {

        if ($results = mysqli_reap_async_query($link)) {

          $idx = array_search(mysqli_thread_id($link), $allLinkThreadIds);

          if ($results !== false) {
            $allResults[$idx] = processDbResults($results); // Custom function
          } else {
            // Attempting to log. This condition is never met.
          }

          freeResults($link); // Custom function
          $processed++;

        }

      }

    }

  }

  ksort($allResults);
  return $allResults;

}

If for some reason the result of mysqli_query is invalid, then the $result variable should be false, right? But the else part of if ($result !== false) never gets satisfied, so is it safe to make the assumption that $result is valid and that $link is a valid MySQLi object?

If that's the case though, then how can I ever get the PHP warning from the mysqli_poll function call?

Any pointers to help guide me in where I'm misunderstanding something would be extremely helpful.
Thanks.

HartleySan
  • 7,404
  • 14
  • 66
  • 119
  • Took a while to figure out, but it turns out that the MySQL DB was actually intermittently going out to lunch, which was causing this problem. Once we resolved the DB issues, this problem went away. – HartleySan Apr 09 '18 at 19:17

0 Answers0