用于测试 pdo 的 php 代码是否可用?

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

php code to test pdo is available?

phpmysqlpdo

提问by ohho

I want to use PDObut I'm not sure whether my hosting has set it up properly.

我想使用PDO,但我不确定我的主机是否已正确设置。

How can I test, in PHP, whether it is setup and working for MySQL?

如何在 PHP 中测试它是否已设置并适用于 MySQL?

回答by Elzo Valugi

PDO is always installed for php 5.1+. You can check for specific db drivers that are installed or not using phpinfo(); You could try to check for specific drivers using @Mark Baker idea and checking for specific constants;

PDO 始终为 php 5.1+ 安装。您可以使用 phpinfo() 来检查已安装或未安装的特定数据库驱动程序;您可以尝试使用@Mark Ba​​ker 想法检查特定驱动程序并检查特定常量;

var_dump(defined(PDO::MYSQL_ATTR_LOCAL_INFILE)); // mysql
var_dump(PDO::FB_ATTR_TIME_FORMAT)); // firebird

Note that not all drivers have specific constants defined so phpinfo() remains best solution.

请注意,并非所有驱动程序都定义了特定的常量,因此 phpinfo() 仍然是最佳解决方案。

Using command line you can check using:

使用命令行,您可以使用以下方法进行检查:

$ php -m

As an alternative of phpinfo() you can use:

作为 phpinfo() 的替代方案,您可以使用:

extension_loaded ('PDO' ); // returns boolean
// or
extension_loaded('pdo_mysql');
// or get all extensions and search for a specific one
get_loaded_extensions(); 

回答by Mark Baker

Aside from using phpinfo() to see if it's correctly listed

除了使用 phpinfo() 来查看它是否被正确列出

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

回答by rizon

Try this

尝试这个

print_r(PDO::getAvailableDrivers());

should give applications supported by PHP

应该提供 PHP 支持的应用程序

Array ( [0] => mysql [1] => sqlite )

回答by Tareq

Using command line, for PDO:

使用命令行,对于 PDO:

php -m|grep -i pdo

For PDO with MySQL support:

对于支持 MySQL 的 PDO:

php -m|grep -i pdo_mysql

To install php mysql support, search for the package name (Ubuntu):

要安装 php mysql 支持,请搜索包名(Ubuntu):

apt-cache search php5*|grep mysql

And install it if not already did (Ubuntu):

如果尚未安装(Ubuntu),请安装它:

sudo apt-get install php5-mysql

回答by DrColossos

Create a file that contains

创建一个文件,其中包含

<?php

phpinfo();

?>

It will show you if PDO (and other features are enabled)

它将显示您是否启用了 PDO(和其他功能)

回答by Dave Morton

For Windows servers, I've found the following useful for checking which PDO extensions are loaded from the command prompt:

对于 Windows 服务器,我发现以下内容对于检查从命令提示符加载了哪些 PDO 扩展非常有用:

php -m|findstr -i pdo_

php -m|findstr -i pdo_

On my system, that command nets me the following results:

在我的系统上,该命令为我提供了以下结果:

pdo_mysql PDO_ODBC pdo_pgsql pdo_sqlite

pdo_mysql PDO_ODBC pdo_pgsql pdo_sqlite