Skip to content

poll

In this article, we will discuss the mysqli_poll() function in PHP, which is used to poll connections.

Introduction to the mysqli_poll() function

The mysqli_poll() function is a built-in function in PHP that is used to poll connections. This function can be used to check if there is any data available on a connection before reading from it. The mysqli_poll() function can be used in conjunction with non-blocking I/O to make your application more responsive.

Important: mysqli_poll() only works with the MySQLND driver. It is not available when using the older libmysql driver.

How to use the mysqli_poll() function

Using the mysqli_poll() function is straightforward. Here's an example:

How to use the mysqli_poll() function?

php
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

// Enable non-blocking mode for asynchronous queries
$mysqli->options(MYSQLI_OPT_NONBLOCK, 1);

$links = [$mysqli];
$error = [];
$reject = [];
$timeout = 10;

while (!empty($links)) {
    // mysqli_poll modifies the $links array by reference, keeping only ready connections
    $ready = mysqli_poll($links, $error, $reject, $timeout, 0);
    
    if ($ready === false) {
        // Handle poll failure or timeout
        echo "Poll failed or timed out.\n";
        break;
    }

    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
            // process the result set
            $result->free();
        } else {
            // handle the error
            echo "Query error: " . $link->error . "\n";
        }
    }
}

$mysqli->close();
?>

In this example, we first create a new MySQLi object using the mysqli() constructor. We then check if the connection was successful using the connect_errno property. If the connection was successful, we enable non-blocking mode via mysqli::options(MYSQLI_OPT_NONBLOCK, 1), which is required for asynchronous queries. We then create an array of links that we want to poll, along with empty $error and $reject arrays. The $error array will be populated with error messages from connections that failed, while $reject will contain connections that actively rejected the poll request. We enter a loop that continues as long as there are connections to poll. Inside the loop, we call mysqli_poll() with the links array (passed by reference), the error/reject arrays, and a timeout of 10 seconds. If there is data available on one or more of the links, mysqli_poll() returns the number of ready connections and modifies the $links array to contain only those ready connections. We then loop through the ready links and call the reap_async_query() method on each to retrieve any available data. If mysqli_poll() returns false, we handle the error or timeout and exit the loop. Finally, we close the connection.

Note: For complex asynchronous workloads, consider modern alternatives like ReactPHP or Swoole, which provide more robust event-loop architectures for PHP.

Conclusion

In conclusion, the mysqli_poll() function is a powerful tool for polling connections in PHP. By understanding how to use the function, you can make your application more responsive and efficient when working with MySQL databases.

Practice

What are the main steps involved in creating a simple poll using PHP?

Dual-run preview — compare with live Symfony routes.