Check Mysql Info using PHP code

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Check Mysql Info using PHP code

Post by it2051229 »

Ok, how do I check mysql Version and other infos without using the code phpinfo();

I'm trying to make a pre-installation check for my project and it requires MySQl, Enabled GD Library, and PHP version 4.3 and above.. how do I do that??
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Check Mysql Info using PHP code

Post by Ollie Saunders »

Code: Select all

if ($result = mysql_query('SELECT VERSION()')) {
    $mysqlVersion = mysql_fetch_object($result);
}
$gdInfo = gd_info();
$gdVersion = $gdInfo['GD Version'];
$phpVersion = PHP_VERSION;
if (stripos(PHP_OS, 'Linux') !== false && file_exists('/etc/issue.net')) {
    $osVersion = file_get_contents('/etc/issue.net');
}
Edit: file_get_contents
Last edited by Ollie Saunders on Tue Jan 15, 2008 5:14 pm, edited 1 time in total.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Check Mysql Info using PHP code

Post by it2051229 »

THANK YOU!
strider72
Forum Newbie
Posts: 2
Joined: Fri Jan 18, 2008 9:37 am

Re: Check Mysql Info using PHP code

Post by strider72 »

On line 2 of your code:

Code: Select all

PHP Parse error:  syntax error, unexpected T_STRING in /Users/Steve/public_html/wp-content/multiblog/mb-functions.php on line 146
Any thoughts? This is (by necessity) running a version check _before_ we've connected to the database, which may be the problem. If so, I'm trying to figure out how to check version before connecting....

(The script in question returns diagnostic info in case of a problem -- so by definition it may run before a connection is made....)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Check Mysql Info using PHP code

Post by Ollie Saunders »

In my code the version is retrieved by a query. You can't query a database without a connection to it. Here's another way you can do it:

Code: Select all

$version = `mysql -V`
This method assumes you have permission to call shell_exec() and that the mysql binary you wish to enquire about is in the system $PATH. You'll need to process $version a bit to get the actual version. Remember to use version_compare() for any conditional logic involving versions that I suspect you will be doing.
strider72
Forum Newbie
Posts: 2
Joined: Fri Jan 18, 2008 9:37 am

Re: Check Mysql Info using PHP code

Post by strider72 »

Thanks for the assist, but no go. This is actually for a diagnostic "help" screen in a software plugin I'm trying to make. As it will be distributed, I can't assume shell access.

Not that important. I'm throwing in an "if" clause and return "not available" if another method doesn't work....

Thanks though. :)
Post Reply