Oracle Database Connection Strings in PHP

It's easy to get confused as to how to specificy your Oracle database connection string,
and there's a handy new feature in Oracle 10g
that makes this a whole lot easier. So here's a little rundown of the three ways to connect to Oracle databases. You can use the:

  • tnsnames.ora file
  • Full connection string
  • Easy connect string
These examples show how to specificy an Oracle connection string using the new OCI8 functions in PHP.

tnsnames.ora File

The tnsnames.ora file is a client side
file that maps an alias used by client programs to a database service. It is used to connect to a non-default database. Here you have to have an entry in the tnsnames.ora file, and
reference the alias to that entry in your connection code.

PHP code:

oci_connect($un, $pw, 'MYDB');

tnsnames.ora entry

MYDB = (DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)
(HOST = mymachine.mydomain)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MYDB.AU.ORACLE.COM)) )

Full Connection String

The full connection string does not require the use of a tnsnames.ora file.
You need to enter the full connection string when you connect to the database in your code.

PHP code:

oci_connect($un, $pw,
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=mymachine.mydomain)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SERVICE_NAME = MYDB)))');

Easy Connect String

This is one Oracle 10g feature that I use daily. As I constantly connect to so many different databases in my day, this has saved me so much time as I don't have to configure anything, just know the machine name and the database alias and I'm off.

The easy connect string does not require the use of a tnsnames.ora file, and is an abbreviated version of the full connection string. you must have the Oracle 10g client-side libraries to use the easy connect string.

PHP code:

oci_connect($un, $pw, '//mymachine.mydomain:port/MYDB');

Comments:

Please tell me how to connect the database using php 4.0?..

Posted by kin on July 29, 2008 at 01:10 PM DDUT #

PHP 4 used ora_logon(), which has now been replaced with oci_connect(). A quick google gave me this page, which should help you. http://www.phpdig.net/ref/rn44re870.html You might want to read the Underground PHP and Oracle Manual. That includes an Appendix that maps the old to new PHP functions, and lots of useful info. http://www.oracle.com/technology/tech/php/pdf/underground-php-oracle-manual.pdf

Posted by Alison Holloway on July 30, 2008 at 02:23 AM DDUT #

Have you managed to configure phporacleview to connect to a remote database ?

Posted by Malcolm Bodger on November 21, 2008 at 07:44 PM DDUT #

where can i get a good ebook on php programming with oracle.

Posted by netsky32 on March 24, 2009 at 10:17 AM DDUT #

Hello, Please help me, I need connection oracle with php, I wrote: $DBConnection=oci_connect(scott, tiger, '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)) (CONNECT_DATA=(SERVER=DEDICATED) (SERVICE_NAME = motion)))'); but, not run Thanks you

Posted by Rocio Zuniga on March 24, 2009 at 06:16 PM DDUT #

Hi,
Actually i want the actual connection with oracle using php with an example can any one help me?

Thanks,
Nilesh G. Pangul

Posted by guest on July 21, 2011 at 05:12 PM DDUT #

hello this is tested script of (select data)oracle10g connect php
<?php

$conn = oci_connect('system','HW#F45RA','localhost:1521/XE');
//system is username
//HW#F45RA is password
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Prepare the statement
$stid = oci_parse($conn, 'SELECT * FROM student');
if (!$stid) {
$e = oci_error($conn);
//trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Fetch the results of the query
print "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
print "<tr>\n";
foreach ($row as $item) {
print " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
}
print "</tr>\n";
}
print "</table>\n";

oci_free_statement($stid);
oci_close($conn);

?>

Posted by pravesh jain on February 17, 2012 at 07:01 PM DDUT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

A blog on Oracle VM, PHP and any other projects I work on.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today