SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting MySQL version

    Does anyone know of a reliable way to find out if MySQL is installed, and if so, what version is installed, on a server just with a PHP script? Ideally this would be done without a valid MySQL username or password. Possible, or am I dreaming?

    Thanks

    Sam

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Open a socket connection to localhost on port 3306. The second thing MySQL sends upon opening a connection is its version string.

    Code:
    [root@www]# telnet localhost 3306
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    4
    5.0.62CrOWHANDH~,bQNqqbwQmP)z
    ^ 5.0.62

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the idea. Is this something that most servers (including shared hosting accounts) would allow? The reason I ask is that this is for a compatibility checker for an application I plan to distribute, and it needs to be as cross-platform compatible as possible (though only for *nix servers).

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't see anything in php.ini or fsockopen's manual page to suggest the function can be disabled by a setting. And if fsockopen can't connect, then mysql_connect wouldn't be able to connect either. I suppose they could run MySQL on a different port than 3306, which you would need to know for configuration anyway.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    You could also look at the mysql_get_*_info functions. Only if using a username/password is acceptable, of course.

    PHP Code:
    $conn   mysql_connect('localhost''username''password');

    echo 
    'Server version   : 'mysql_get_server_info($conn), "\n";
    echo 
    'Client library   : 'mysql_get_client_info(), "\n";
    echo 
    'Protocol version : 'mysql_get_proto_info($conn), "\n";
    echo 
    'Host info        : 'mysql_get_host_info($conn), "\n"
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone. Your help is much appreciated

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Open a socket connection to localhost on port 3306. The second thing MySQL sends upon opening a connection is its version string.

    Code:
    [root@www]# telnet localhost 3306
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    4
    5.0.62CrOWHANDH~,bQNqqbwQmP)z
    ^ 5.0.62
    Great tip Dan, so given that, I'm assuming something like this would suffice.
    PHP Code:
    <?php
    function getMySQLServerVersion($sServer$iPort 3306)
    {
        if(
    $rHandle fsockopen($sServer$iPort))
        {
            if(
    === preg_match('~[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}~'fread($rHandle512), $aMatches))
            {
                
    fclose($rHandle);
                return 
    $aMatches[0];
            }
            
    fclose($rHandle);
        }
        return 
    false;
    }

    echo 
    getMySQLServerVersion('localhost');

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •