Saturday, June 16, 2007

PHP Connection to Oracle

How to do this? In this post, I'll try to write something make me got a headache for several days. But it's okey, I've solved it and I wanna share to you all. First, we have to install Oracle Client in our computer (will be explained in next post). After that, go to D:\Oracle\Network\Admin and you will find a file named tnsnames.ora. Here is the key of the connection. Open it with text editor you have and now we will see some simple codes here:

$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)
(HOST = 10.126.11.15)(PORT = 1521)))(CONNECT_DATA=(SID=ORAPBD7)))";
if ($c=OCILogon("username", "password", $db)) {
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
} else {
$err = OCIError();
echo "Connection failed." . $err[text];

You can find the connection string "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.126.11.15)(PORT = 1521)))(CONNECT_DATA=(SID=ORAPBD7)))" in file named tnsnames.ora. In this connection, the Oracle server has ip address 10.126.11.15 with default port 1521. "ORAPBD7" is the name of database in the server that we'll manipulate later. You can save the php file in your web server (like Apache or IIS). Suppose we save it as "connection.php" and to access this file, type http://localhost/connection.php. If connection successfull, the browser will print "Successfully connected to Oracle.". Unless, you will get a message "Connection failed.".

Okey, it's something I can write for you today. You will find my post in this blog too. Thanks for reading.

Life is full with light of grey. Make it colourful with a pray. Good luck!


Special thanks for my best friend: Abid

2 comments:

Anonymous said...

Connection and other PHP and Oracle tips are discussed in the free Underground PHP and Oracle manual at http://www.oracle.com/technology/tech/php/pdf/underground-php-oracle-manual.pdf

Stu said...

It's very nice to hear I've a friend like you. What's your blog Chris?