In this tutorial, I try to connect to Oracle with PHP from a Windows machine.
The Software
These are the lists of software I used to test this tutorial:
- XAMPP 1.6.7
- Oracle Instant Client 10.1 for Windows
- Oracle 8i Database Server (remote or local)
These are the steps to preparing your system before you can connect to Oracle using PHP.
- Install XAMPP.
- Install Oracle if you want to use a local database server.
- Extract the Oracle Instant Client to C:
- Copy oci.dll file to C:\xampp\apache\bin.
- Add the Oracle Instant Client path (C:\instantclient10_1) to your system PATH.
- Add NLS_LANG as a new system variable. Fill it's value with american_america.we8iso8859p1.
- Restart your system to make the changes take effect.
php.ini file for XAMPP can be found in C:\xampp\apache\bin. Open that file with your text editor and find this following line:
;extension=php_oci8.dll
Uncomment that line so it will be looked like this:
extension=php_oci8.dll
Save that file and then restart your Apache web server. If no error message appear that mean the Oracle module for PHP is loaded properly.
Test It!
Now, prepare the PHP script to test the connection with Oracle. I made the script like this:
$host='10.234.2.12';
$user='USER';
$passwd='123456';
$service_name='TEST';
$db='(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
(HOST='.$host.')(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME = '.$service_name.')))';
$con=oci_connect($user,$passwd,$db);
if($con){
echo 'Connected to Oracle.';
}
else{
echo 'Cannot connect to Oracle.';
}
oci_close($con);
$user='USER';
$passwd='123456';
$service_name='TEST';
$db='(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
(HOST='.$host.')(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME = '.$service_name.')))';
$con=oci_connect($user,$passwd,$db);
if($con){
echo 'Connected to Oracle.';
}
else{
echo 'Cannot connect to Oracle.';
}
oci_close($con);
Run the script and see if it works.
4 comments:
Great tutorial :)
Ahh Great ..this is wt I wanted
Nice Sharing Dear Keep it up....
I try this but error "Warning: oci_connect() [function.oci-connect]: ORA-03134: Connections to this server version are no longer supported. in D:\xampp\htdocs\test\test.php on line 10
Cannot connect to Oracle.
Warning: oci_close() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\test\test.php on line 19"
do you know this solution?
Post a Comment