如何使用 php 检测机器是否安装了 oracle(oci8 和/或 pdo_oci)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/111440/
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
How can I detect, using php, if the machine has oracle (oci8 and/or pdo_oci) installed?
提问by John Fiala
How can I detect, using php, if the machine has oracle (oci8 and/or pdo_oci)
installed?
如何使用 php 检测机器是否(oci8 and/or pdo_oci)
安装了oracle ?
I'm working on a PHP
project where some developers, such as myself, have it installed, but there's little need for the themers to have it. How can I write a quick function to use in the code so that my themers are able to work on the look of the site without having it crash on them?
我正在PHP
开发一个项目,其中一些开发人员(例如我自己)安装了它,但主题人员几乎不需要安装它。如何编写一个快速函数以在代码中使用,以便我的主题能够处理网站的外观,而不会使其崩溃?
采纳答案by Greg
if the oci extension isn't installed, then you'll get a fatal error with farside.myopenid.com's answer, you can use function_exists('oci_connect') or extension_loaded('oci8') (or whatever the extension's actually called)
如果未安装 oci 扩展,则您将收到 farside.myopenid.com 的答案的致命错误,您可以使用 function_exists('oci_connect') 或 extension_loaded('oci8') (或任何扩展实际调用的内容)
回答by Nathan Strong
The folks here have pieces of the solution, but let's roll it all into one solution.
这里的人有一些解决方案,但让我们将它们全部合并为一个解决方案。
For just a single instance of an oracle function, testing with function_exists()
is good enough; but if the code is sprinkled throughout to OCI calls, it's going to be a huge pain in the ass to wrap every one in a function_exists()
test.
对于一个 oracle 函数的单个实例,测试function_exists()
就足够了;但是如果将代码散布到 OCI 调用中,那么将每个代码都包含在function_exists()
测试中将是一件非常痛苦的事情。
Therefore, I think the simplest solution would be to create a file called nodatabase.phpthat might look something like this:
因此,我认为最简单的解决方案是创建一个名为nodatabase.php的文件,它可能如下所示:
<?php
// nodatabase.php
// explicitly override database functions with empty stubs. Only include this file
// when you want to run the code without an actual database backend. Any database-
// related functions used in the codebase must be included below.
function oci_connect($user, $password, $db = '', $charset='UTF-8', $session_mode=null)
{
}
function oci_execute($statement, $mode=0)
{
}
// and so on...
Then, conditionally include this file if a global (say, THEME_TESTING) is defined just ahead of where the database code is called. Such an include might look like this:
然后,如果在调用数据库代码的位置之前定义了全局(例如 THEME_TESTING),则有条件地包含此文件。这样的包含可能如下所示:
// define("THEME_TESTING", true) // uncomment this line to disable database usage
if( defined(THEME_TESTING) )
include('nodatabase.php'); // override oracle API with stub functions for the artists.
Now, when you hand the project over to the artists, they simply need to make that one modification and they're good to go.
现在,当您将项目交给美工时,他们只需要进行一次修改就可以了。
回答by Nathan Strong
As mentioned above by Greg, programmatically you can use the function_exists() method. Don't forget you can also use the following to see all the environment specifics with your PHP install using the following:
正如 Greg 上面提到的,您可以以编程方式使用 function_exists() 方法。不要忘记,您还可以使用以下命令查看 PHP 安装的所有环境细节:
<?php
phpinfo();
?>
回答by user19264
I dont know if I fully understand your question but a simple way would be to do this:
我不知道我是否完全理解你的问题,但一个简单的方法是这样做:
<?php
$connection = oci_connect('username', 'password', 'table');
if (!$connection) {
// no OCI connection.
}
?>