如何确定在 PHP 中是否启用了 PDO?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6113262/
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-08-25 23:23:25  来源:igfitidea点击:

How to determine if PDO is enabled in PHP?

phppdo

提问by H. Ferrence

Is there a PHP command I can use to determine if PDO is enabled or disabled?

是否有可用于确定 PDO 是启用还是禁用的 PHP 命令?

I know I an manually run phpinfo() and eyeball it, but I have a script I run various web servers that display selected PHP configuration settings for the server.

我知道我手动运行 phpinfo() 并观察它,但我有一个脚本运行各种 Web 服务器,显示服务器的选定 PHP 配置设置。

So I am looking to see if there is a command I can use.

所以我想看看是否有我可以使用的命令。

回答by Salman von Abbas

The proper way of determining that will be using the extension_loadedfunction:-

确定将使用extension_loaded函数的正确方法:-

if ( extension_loaded('pdo') ) {
    .......
}

And you might also want to check for the database-specific PDO driver using:-

您可能还想使用以下方法检查特定于数据库的 PDO 驱动程序:-

if ( extension_loaded('pdo_<database type here>') ) { // e.g., pdo_mysql
    .......
}

回答by GolezTrol

Check if the class exists:

检查类是否存在:

if (class_exists('PDO'))
if (class_exists('PDO'))

I appreciate the support and all the upvotes I still get, but please check Salman Abbas's answerfor the properway to do this.

我感谢我仍然得到的支持和所有赞成票,但请查看Salman Abbas 的答案以了解正确的方法。

回答by Prashant Agrawal

Just run the command as php -mfrom the command prompt which will display list of modules installed for PHP

只需在命令提示符下以php -m 的形式运行该命令,该命令将显示为 PHP 安装的模块列表

回答by Alix Axel

You have two options:

您有两个选择:

if (extension_loaded('pdo')) { /* ... */ }

Or (this one is not 100% reliable since it can be implemented in user-land classes):

或者(这个不是 100% 可靠的,因为它可以在用户空间类中实现):

if (class_exists('PDO', false)) { /* ... */ }

Personally, I prefer the first option.

就我个人而言,我更喜欢第一个选项。

回答by krishna

if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO available';
}

I hope this works

我希望这有效

回答by Marc B

How about

怎么样

if (in_array('pdo', get_loaded_extensions())) {
   ... pdo is there ...
}

回答by A.Barth

An option that works for any module is

适用于任何模块的选项是

php -i | grep -i "Your module name"

This allows you to know if your module is enabled or not, but you should still try it out.

这使您可以知道您的模块是否已启用,但您仍然应该尝试一下。