oracle 如何连接到Oracle数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87769/
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 to connect to Oracle database?
提问by
How do you connect to Oracle using PHP on MAC OS X?
如何在 MAC OS X 上使用 PHP 连接到 Oracle?
回答by Rich Adams
The PDO abstraction layer can be used to connect to, and perform actions on, an Oracle DB. Here's an articleon how to use PDO with Oracle from the Oracle website.
PDO 抽象层可用于连接到 Oracle DB 并对其执行操作。这是Oracle 网站上有关如何将 PDO 与 Oracle 结合使用的文章。
It's also possible to use OCI.
也可以使用OCI。
The Oracle PHP Development Centrewill have lots more useful information on using Oracle and PHP together.
在甲骨文PHP开发中心将有很多关于使用Oracle和PHP一起更多有用的信息。
回答by nick fox
For instantclient on osx 10.6 64bit do the following:
对于 osx 10.6 64 位上的 Instantclient,请执行以下操作:
download the instant client librarys and sdk, stuff it all in a folder. Make sure you get the 64 bit library if you are on a 64 bit machine, 32bit won't work! - test with sqlplus first
下载即时客户端库和 sdk,将它们全部放入一个文件夹中。如果您使用的是 64 位机器,请确保获得 64 位库,32 位将无法工作!- 先用 sqlplus 测试
create this if it does not exist
如果它不存在就创建它
sudo vi /etc/launchd.conf
and add to following to the file(with your own path!)
并添加到文件中(使用您自己的路径!)
setenv DYLD_LIBRARY_PATH /usr/oracle_instantClient64
You probaby need to restart your system at this point for launchd to pass the path to apache to pick up the path, or see if restarting launchd works, though i have a feeling that will restart your system anyway!
您可能此时需要重新启动系统,以便launchd 将路径传递给apache 以获取路径,或者查看重新启动launchd 是否有效,尽管我有一种感觉无论如何都会重新启动您的系统!
You should add "extension=oci8.so" to php.ini
您应该将“extension=oci8.so”添加到 php.ini
sudo vi /etc/php.ini
if that file does not exist copy php.ini.default
如果该文件不存在复制 php.ini.default
sudo cp /etc/php.ini.default /etc/php.ini
then add the above extension, there is a section with lots of extensions further down the file, put it there somewhere
然后添加上面的扩展名,在文件的下方有一个有很多扩展名的部分,把它放在某个地方
oci requires a library symlink so do
oci 需要一个库符号链接,所以这样做
sudo ln -s $DYLD_LIBRARY_PATH/libclntsh.dylib.10.1 $DYLD_LIBRARY_PATH/libclntsh.dylib
Also theres some wierd hardcoded library link in the oracle binaries so fix that
oracle 二进制文件中还有一些奇怪的硬编码库链接,因此请修复它
mkdir -p /b/227/rdbms/
Its only looking for the oracle libraries so link it back
它只是在寻找 oracle 库,因此将其链接回
ln -s /usr/oracle_instantClient64/ /b/227/rdbms/lib
now install oci8 from pear repository. If you have installed snow leopard osx 10.6 without upgrading you may have problems with pear and pecl. If so you will need to install pear first. see: https://discussions.apple.com/thread/2602597?start=0&tstart=0
现在从 pear 存储库安装 oci8。如果您在没有升级的情况下安装了雪豹 osx 10.6,您可能会遇到 pear 和 pecl 的问题。如果是这样,您需要先安装梨。见:https: //discussions.apple.com/thread/2602597?start=0&tstart=0
sudo pecl install oci8
HINT: don't use autodetect, specify the instantclient path when it asks you..
提示:不要使用自动检测,当它询问您时指定即时客户端路径..
instantclient,/usr/oracle_instantClient64
restart apache
重启阿帕奇
sudo apachectl graceful
test by navigating to the URL in a browser or you can call the file directly on the command line
通过在浏览器中导航到 URL 进行测试,或者您可以直接在命令行上调用该文件
php index.php
thats it use the following as a test file..
这就是它使用以下作为测试文件..
<?php
$dbHost = "localhostOrDatabaseURL";
$dbHostPort="1521";
$dbServiceName = "servicename";
$usr = "username";
$pswd = "password";
$dbConnStr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST=".$dbHost.")(PORT=".$dbHostPort."))
(CONNECT_DATA=(SERVICE_NAME=".$dbServiceName.")))";
if(!$dbConn = oci_connect($usr,$pswd,$dbConnStr)){
$err = oci_error();
trigger_error('Could not establish a connection: ' . $err['message'], E_USER_ERROR);
};
$strSQL = "SELECT SYSDATE FROM DUAL";
$stmt = oci_parse($dbConn,$strSQL);
if ( ! oci_execute($stmt) ){
$err = oci_error($stmt);
trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
};
while(oci_fetch($stmt)){
$rslt = oci_result($stmt, 1); print "<h3>query returned: ".$rslt."</h3>";
}
?>
回答by Guy
I dont know the Mac specifically, nor PHP, but you usually need to install the Oracle Client tools (Instant Client).
我不具体了解 Mac,也不了解 PHP,但您通常需要安装 Oracle 客户端工具(即时客户端)。
http://www.oracle.com/technology/tech/oci/instantclient/index.html
http://www.oracle.com/technology/tech/oci/instantclient/index.html
Once installed you modify the TNSNAMES.ORA file to point to the server and instance name of the Oracle database.
安装后,您修改 TNSNAMES.ORA 文件以指向 Oracle 数据库的服务器和实例名称。
Then you can use the PHP "database connection" stuff (sorry) to create a connection and running your SQL statements.
然后你可以使用PHP“数据库连接”的东西(对不起)来创建一个连接并运行你的SQL语句。
Use the SQL*PLUS client to check the connection works:
使用 SQL*PLUS 客户端检查连接是否正常:
ie.
IE。
c:> SQLPLUS
CONNECT scott/tiger@mydatabase
If the TNSNAMES.ORA is correct you should get a connection, or at least "username/password incorrect" that proves you got communication with the Oracle instance.
如果 TNSNAMES.ORA 是正确的,您应该获得一个连接,或者至少是“用户名/密码不正确”,以证明您与 Oracle 实例进行了通信。
If you get TNS-12521 (?) errors then your TNSNAMES.ORA is incorrect.
如果您收到 TNS-12521 (?) 错误,则您的 TNSNAMES.ORA 不正确。
回答by bastiandoeen
Connecting to an oracle database should be no problem with the oci-interface, using "oci_connect()" for example.
使用 oci-interface 连接到 oracle 数据库应该没有问题,例如使用“oci_connect()”。
Further examples are here: http://php.net/manual/en/oci8.setup.php
更多例子在这里:http: //php.net/manual/en/oci8.setup.php
But I do not understand, what the remark MAC OS X means - are you running an apache locally?
但我不明白,MAC OS X 这句话的意思是什么 - 你在本地运行 apache 吗?
Hope this helps, Bastian
希望这会有所帮助,巴斯蒂安