W3docs

get_server_version

In this article, we will focus on the mysqli_get_server_version() function in PHP, which is used to return the version number of the MySQL server. We will

In this article, we will focus on the mysqli_get_server_version() function in PHP, which returns the version number of the MySQL server as an integer. We will explain how it works, how to decode the number it returns, when to use it instead of the string-based mysqli_get_server_info(), and the common gotchas to watch out for.

Introduction to the mysqli_get_server_version() function

The mysqli_get_server_version() function is a built-in PHP function that returns the version of the connected MySQL server as a single integer. It takes one argument — an open MySQLi connection — and returns a number such as 80037.

int mysqli_get_server_version(mysqli $mysql)

The integer is encoded with this formula:

main_version * 10000 + minor_version * 100 + sub_version

So MySQL 8.0.37 becomes 8 * 10000 + 0 * 100 + 37 = 80037, and MySQL 5.7.29 becomes 50729. Because the value is a plain integer, it is the most reliable form to use in numeric comparisons — you do not have to parse a string like "8.0.37-0ubuntu0.22.04.1".

How to use the mysqli_get_server_version() function

Call the function on a valid MySQLi connection. Always confirm the connection succeeded first, otherwise you would be passing an invalid handle:

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

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$server_version = mysqli_get_server_version($mysqli);

echo "Server version (integer): " . $server_version;

mysqli_close($mysqli);
?>

Here we open a connection with mysqli_connect(), verify it, then read the version. The output for a MySQL 8.0.37 server is:

Server version (integer): 80037

The function also works in object-oriented style — $mysqli->server_version is the property equivalent of the procedural call.

Decoding the version number

Because the result is encoded, you usually want to turn it back into a human-readable MAJOR.MINOR.PATCH string. Reverse the formula with integer division and the modulo operator:

<?php
$version = 80037; // value returned by mysqli_get_server_version()

$major = intdiv($version, 10000);
$minor = intdiv($version % 10000, 100);
$patch = $version % 100;

echo "MySQL {$major}.{$minor}.{$patch}";
?>

This prints:

MySQL 8.0.37

When to use it (and what to use instead)

  • Use mysqli_get_server_version() when you need to compare versions in code — for example, to enable a feature only on MySQL 8.0 or newer. A numeric check like if ($mysqli->get_server_version() >= 80000) is simpler and safer than string parsing.
  • Use mysqli_get_server_info() when you want the full human-readable version string (including distribution suffixes) for logging or display.
  • Use mysqli_get_client_version() when you need the version of the MySQL client library PHP was compiled against, rather than the server it is talking to.

Common gotchas

  • The function returns the version of the server, not of PHP itself. For the PHP runtime version, use phpversion() or the PHP_VERSION constant.
  • It requires an established connection. If mysqli_connect() fails, check mysqli_connect_error() before calling this function.
  • The integer is not the same value as the string from mysqli_get_server_info(); don't compare the two directly.

Conclusion

The mysqli_get_server_version() function provides a reliable, numeric way to check the MySQL server version. Knowing the main * 10000 + minor * 100 + sub encoding lets you decode it back into a readable string and write clean version comparisons that keep your database-driven applications compatible and stable.

Practice

Practice
What does mysqli_get_server_version() return for a MySQL 8.0.37 server?
What does mysqli_get_server_version() return for a MySQL 8.0.37 server?
Was this page helpful?