Current Location: Home> Latest Articles> How to Check MySQL Version in PHP Scripts Using mysql_get_server_info and Perform Corresponding Actions

How to Check MySQL Version in PHP Scripts Using mysql_get_server_info and Perform Corresponding Actions

gitbox 2025-09-11

How to Check MySQL Version in PHP Scripts Using mysql_get_server_info and Perform Corresponding Actions

In PHP development, it is often necessary to check the MySQL database version to execute specific actions based on different versions. The MySQL version can affect certain SQL queries, functions, or features, so knowing how to dynamically adjust your PHP script according to the MySQL version is crucial.

In this article, we will demonstrate how to use the mysql_get_server_info function in PHP scripts to check the MySQL version and perform actions based on the version information.

1. Getting MySQL Version Using mysql_get_server_info

mysql_get_server_info is a built-in PHP function that retrieves the MySQL server version. This function is available in PHP 5.x and earlier MySQL extensions. Note that in PHP 7 and later, the MySQL extension is deprecated, and it is recommended to use the mysqli or PDO_MySQL extensions.

First, make sure you are properly connected to the MySQL database. Once connected, you can call the mysql_get_server_info function to get the server version.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Connect to MySQL database</span></span><span>
</span><span>$connection = </span><span><span class="hljs-title function_ invoke__">mysql_connect</span></span>('localhost', 'root', 'password');
</span><span>if (!$connection) {
    die('Could not connect to database: ' . <span class="hljs-title function_ invoke__">mysql_error()</span>());
}
<p></span>// Get MySQL version<br>
</span>$mysql_version = mysql_get_server_info($connection);</p>
<p>// Output MySQL version<br>
</span>echo "Current MySQL version is: " . $mysql_version;</p>
<p>// Close the database connection<br>
</span>mysql_close($connection);<br>
?><br>
</span>

2. Getting MySQL Version Using mysqli Extension

As mentioned earlier, mysql_get_server_info is deprecated, so it is recommended to use the mysqli extension instead. Here is how to get the MySQL version using mysqli:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span>// Connect to MySQL database</span>
</span><span>$connection = </span><span><span class="hljs-title function_ invoke__">mysqli_connect</span>('localhost', 'root', 'password');
</span>if (!$connection) {
    die('Could not connect to database: ' . <span class="hljs-title function_ invoke__">mysqli_connect_error()</span>());
}
<p></span>// Get MySQL version<br>
</span>$mysql_version = </span>mysqli_get_server_info($connection);</p>
<p>// Output MySQL version<br>
</span>echo "Current MySQL version is: " . $mysql_version;</p>
<p></span>// Close the database connection<br>
</span>mysqli_close($connection);<br>
?><br>
</span>

3. Performing Different Actions Based on MySQL Version

Once you have obtained the MySQL version, you can perform different actions based on the version number. Some features may only be available in higher MySQL versions and not in lower ones. You can use conditional statements to check the MySQL version and execute different actions accordingly.

Here is an example showing how to decide whether to use certain features based on the MySQL version:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span>// Connect to MySQL database</span>
</span>$connection = </span><span><span class="hljs-title function_ invoke__">mysqli_connect</span>('localhost', 'root', 'password');
</span>if (!$connection) {
    die('Could not connect to database: ' . <span class="hljs-title function_ invoke__">mysqli_connect_error()</span>());
}
<p></span>// Get MySQL version<br>
</span>$mysql_version = </span>mysqli_get_server_info($connection);</p>
<p>// Check MySQL version<br>
</span>if (version_compare($mysql_version, '5.7', '>=') ) {<br>
echo "MySQL version is 5.7 or higher, new features can be used.";<br>
// Execute actions related to new features<br>
} else {<br>
echo "MySQL version is below 5.7, executing legacy operations.";<br>
// Execute actions related to older versions<br>
}</p>
<p></span>// Close the database connection<br>
</span>mysqli_close($connection);<br>
?><br>
</span>

4. Version Comparison and Handling

version_compare is a very useful PHP function for comparing two version numbers. It returns three possible values:

  • Returns 1 if the first version is greater than the second;

  • Returns 0 if the versions are equal;

  • Returns -1 if the first version is less than the second.

This allows you to flexibly take different actions based on the MySQL version.

5. Important Notes

  • When using mysql_get_server_info or mysqli_get_server_info, ensure that the connection between PHP and the MySQL database is successful; otherwise, these functions will not work properly.

  • It is recommended to use mysqli or PDO_MySQL, especially in PHP 7 and later, as the mysql extension has been deprecated.

  • When checking MySQL versions, you can use regular expressions to handle complex version strings for more accurate logic.

Using the above methods, you can easily perform different actions in PHP scripts based on the MySQL version, ensuring your program is compatible with multiple MySQL versions and enhancing the flexibility and robustness of your code.