-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP 8.4 | wpdb::check_connection(): handle deprecation of mysqli_ping() (Trac 62061) #7374
Conversation
The `mysqli_ping()` function is deprecated as of PHP 8.4, though, in reality, the function wasn't working according to spec anymore since PHP 8.2 when the `libmysql` driver was dropped in favour of `libmysqlnd`, which was already the default (and recommended) driver since PHP 5.4. The `mysqli_ping()` method was also not really correctly named as its functionality was to reconnect to the database, not just ping. The alternative is to "manually" ping the database by sending a `DO 1` query (the cheapest possible SQL query). Now, I considered adding a PHP version based toggle, but as mentioned above, the default driver has been `libmysqlnd` since PHP 5.4 and in that case, the function never worked anyway, so in reality `mysqli_ping()` was only really functional for the odd custom PHP compilation where `mysqli` was build against `libmysql` AND `reconnect` was not disabled. With this in mind, I'm proposing to replace the call to `mysqli_ping()` with the `DO 1` query completely. If that query succeeds, we can conclude the database connection is still alive. This solution should be the most stable as it will work for both PHP 7.2 <= 8.1, independently of which driver `mysqli` was compiled with, as well as for PHP 8.2+. Note: It could also be considered to remove the function call to `mysqli_ping()` completely and rely on standard error handling in case the connection would have dropped, as after all, the fact that the connection existed at the moment the "ping" happened, is no guarantee that the connection will still exist when the next query is send.... I choose not to do so as WP has custom error handling and does not use the PHP native mysqli exceptions for this, which would make implementing this more awkward. Includes a test to verify that the connection check works when there is a valid connection (this was previously not covered by tests). Refs: * https://wiki.php.net/rfc/deprecations_php_8_4#mysqli_ping_and_mysqliping * https://github.com/php/php-src/pull/ 11912#issuecomment-1671762583 * https://stackoverflow.com/questions/2546868/cheapest-way-to-determine-if-a-mysql-connection-is-still-alive/2546922#2546922 * php/php-src 11945 * https://wiki.php.net/rfc/mysqli_support_for_libmysql * https://www.php.net/mysqli_ping
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasoning and approach make sense. LGTM for commit 👍
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Committed via https://core.trac.wordpress.org/changeset/59069. |
The
mysqli_ping()
function is deprecated as of PHP 8.4, though, in reality, the function wasn't working according to spec anymore since PHP 8.2 when thelibmysql
driver was dropped in favour oflibmysqlnd
, which was already the default (and recommended) driver since PHP 5.4.The
mysqli_ping()
method was also not really correctly named as its functionality was to reconnect to the database, not just ping.The alternative is to "manually" ping the database by sending a
DO 1
query (the cheapest possible SQL query).Now, I considered adding a PHP version based toggle, but as mentioned above, the default driver has been
libmysqlnd
since PHP 5.4 and in that case, the function never worked anyway, so in realitymysqli_ping()
was only really functional for the odd custom PHP compilation wheremysqli
was build againstlibmysql
ANDreconnect
was not disabled.With this in mind, I'm proposing to replace the call to
mysqli_ping()
with theDO 1
query completely. If that query succeeds, we can conclude the database connection is still alive. This solution should be the most stable as it will work for both PHP 7.2 <= 8.1, independently of which drivermysqli
was compiled with, as well as for PHP 8.2+.Note: It could also be considered to remove the function call to
mysqli_ping()
completely and rely on standard error handling in case the connection would have dropped, as after all, the fact that the connection existed at the moment the "ping" happened, is no guarantee that the connection will still exist when the next query is send.... I choose not to do so as WP has custom error handling and does not use the PHP native mysqli exceptions for this, which would make implementing this more awkward.Includes a test to verify that the connection check works when there is a valid connection (this was previously not covered by tests).
Refs:
Trac ticket: https://core.trac.wordpress.org/ticket/62061
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.