Get MySQL Server Version with PDO

a 夏天 提交于 2019-12-31 02:40:20

问题


I'm building out an app in Laravel 5 and I need to ensure that one of my tables will be able to perform FULLTEXT searches.

I'd like to detect the MySQL version number(ensuring it's at least 5.6.10 or above) so that I can switch the engine to MyISAM in my migration file for a given table, if that condition fails.

I can't seem to find any docs on how to access the MySQL server info using PDO, which is what Laravel uses out of the box.

Any help is appreciated.


回答1:


Why not just run select version(); query, which will give you all-correct result no matter which API you are using?

echo $pdo->query('select version()')->fetchColumn();

is throwing me not a single error




回答2:


You can use PDO::getAttribute(PDO::ATTR_SERVER_VERSION) http://php.net/manual/en/pdo.getattribute.php




回答3:


A little improved version of Vince Kronlein's answer

function version($min){
    $pdo = DB::connection()->getPdo();
    $version = $pdo->query('select version()')->fetchColumn();

    preg_match("/^[0-9\.]+/", $version, $match);

    $version = $match[0];

    return (version_compare($version, $min) >= 0);
}

Usage:

if(version('5.7.0')){
    //do someting
}



回答4:


I wrote a little method in my migration file:

protected static function version() 
{
    $pdo     = DB::connection()->getPdo();
    $version = $pdo->query('select version()')->fetchColumn();

    (float)$version = mb_substr($version, 0, 6);

    if ($version < '5.6.10') {
        return false;
    }

    return true;
}

Works a charm. Thanks for the comments.



来源:https://stackoverflow.com/questions/31788297/get-mysql-server-version-with-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!