简体   繁体   中英

How to connect to Oracle database?

如何在MAC OS X上使用PHP连接到Oracle?

I would think OCI would be the way to go. PHP has a module for it.

The PDO abstraction layer can be used to connect to, and perform actions on, an Oracle DB. Here's an article on how to use PDO with Oracle from the Oracle website.

It's also possible to use OCI .

The Oracle PHP Development Centre will have lots more useful information on using Oracle and PHP together.

For instantclient on osx 10.6 64bit do the following:

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

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!

You should add "extension=oci8.so" to php.ini

sudo vi /etc/php.ini

if that file does not exist copy 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

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

mkdir -p /b/227/rdbms/

Its only looking for the oracle libraries so link it back

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

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

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>";
}
?>

I dont know the Mac specifically, nor PHP, but you usually need to install the Oracle Client tools (Instant Client).

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.

Then you can use the PHP "database connection" stuff (sorry) to create a connection and running your SQL statements.

Use the SQL*PLUS client to check the connection works:

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.

If you get TNS-12521 (?) errors then your TNSNAMES.ORA is incorrect.

Connecting to an oracle database should be no problem with the oci-interface, using "oci_connect()" for example.

Further examples are here: 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?

Hope this helps, Bastian

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM