20

I need to echo the MySQL version in a PHP script (it's a server requirement check page for users before downloading the plugin) without having them connect to their database.

The upload this script to their server and open it in the browser. I could ask them to run php info but all I need is the Mysql version and it gets formatted in the script with the rest of the results.

How should i go about this?

1
  • 8
    Until you have connected to a mysql server, there is no way to find out what version any server may or may not be running. The best you can hope for is to find the version of the client that is installed not the server version. Commented May 2, 2012 at 13:37

4 Answers 4

25

If you have access to the command line mysql executable you can try this:

function getMySQLVersion() { 
  $output = shell_exec('mysql -V'); 
  preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version); 
  return $version[0]; 
}

For the client version:

print mysql_get_client_info();

Without shell access to get the server version you must connect first:

$link = mysql_connect("localhost", "username", "password");
if (!$link) die('Could not connect: ' . mysql_error());
print "MySQL server version: " . mysql_get_server_info();
mysql_close($link);
Sign up to request clarification or add additional context in comments.

10 Comments

That will be the client version. A lot of hosts have the server on a separate machine.
Unless your wanting the MySQL client version and not the server version? The MySQL client version is what shows up in a phpinfo() request.
Nice. mysql_get_server_info() is easier in PHP than SHOW VARIABLES.
Just an updated side-note. As of PHP 5.5.0 all mysql_* seems to become deprecated. And so, the mysql-get-client-info function. Take a look here.
mysqli version: $mysqli = new mysqli("localhost", "username", "password"); if (mysqli_connect_errno()) die('Could not connect: ' . mysqli_connect_error()); print "MySQL server version: " . $mysqli->server_info; $mysqli->close();
|
16

You'll need the user to enter their DB credentials, so you can connect to the MySQL server and run the following query to get the MySQL server version:

SHOW VARIABLES LIKE 'version'

Here's the output on my server:

Variable_name     Value
----------------------------
version           5.1.53-log

1 Comment

Well thanks guys. I'm going to have to make them enter their DB info I guess.
3

Information output by phpinfo should be accurate. Just add the following to a file & visit it in the browser:

<?php phpinfo(); & find the mysql version under mysql >> Client API version

enter image description here

2 Comments

phpinfo() reports "Client API library version => mysqlnd 5.0.12-dev" but the actual MySQL version is 5.6.33.
Seems odd @bancer were you able to find an explanation? Was it inside of a VM?
2

mysql_get_client_info() is deprecated. For PHP 7 and above you have to use:

echo mysqli_get_client_info();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.