laravel 使用 PDO 获取 MySQL 服务器版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31788297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:06:32  来源:igfitidea点击:

Get MySQL Server Version with PDO

phpmysqllaravelpdo

提问by Vince Kronlein

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.

我正在 Laravel 5 中构建一个应用程序,我需要确保我的一个表能够执行 FULLTEXT 搜索。

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.

我想检测 MySQL 版本号(确保它至少为 5.6.10 或更高版本),以便我可以在给定表的迁移文件中将引擎切换到 MyISAM,如果该条件失败。

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.

我似乎找不到任何关于如何使用 PDO 访问 MySQL 服务器信息的文档,这是 Laravel 开箱即用的。

Any help is appreciated.

任何帮助表示赞赏。

回答by Your Common Sense

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

为什么不直接运行select version();查询,无论您使用哪种 API,它都会为您提供完全正确的结果?

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

is throwing me not a single error

没有给我一个错误

回答by gbestard

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

您可以使用PDO::getAttribute(PDO::ATTR_SERVER_VERSION)http://php.net/manual/en/pdo.getattribute.php

回答by balping

A little improved version of Vince Kronlein's answer

Vince Kronlein 答案的改进版

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
}

回答by Vince Kronlein

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.

很有魅力。感谢您的评论。