How to get complete MySQL version in WordPress

Featured Image

There are two ways of getting MySQL version in WordPress. First ( popular ) is to use $wpdb global object and its db_version() method. Second ( less known ) is to use $wpdb global object combined with its two properties use_mysqli and dbh plus two PHP functions mysql_get_server_info() and mysqli_get_server_info().

Before going any further, let's have a look at the code of method 1:

global $wpdb;
$mysqlVersion = $wpdb->db_version();

And now, the code of method 2:

global $wpdb;
if ( empty( $wpdb->use_mysqli ) ) {
    $mysqlVersion = mysql_get_server_info();
} else {
    $mysqlVersion = mysqli_get_server_info( $wpdb->dbh );
}

or using ternary expression:

global $wpdb;
mysqlVersion = empty( $wpdb->use_mysqli ) ? mysql_get_server_info() : mysqli_get_server_info( $wpdb->dbh );

Which method would you choose? First, right? It is so simple and appealing. But wait a minute. Let's consider two scenarios. First - real MySQL version '5.6.23', second - real MySQL version '5.1.56-log'. In first scenario results of both methods are identical - '5.6.23'. In second scenario - method 1 gives you '5.1.56' and method 2 gives you '5.1.56-log'. Now, which method would you choose before calling Oracle for support?
truculent-ending

Leave a Reply

Your email address will not be published. Required fields are marked *

*