PHP Oracle (php diinstall diserver database oracle)

31 Jul

PHP Oracle

Batasan :
========================================================================
a. server sudah terinstall oracle database
b. konfigurasi
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1; export ORACLE_HOME
c. listener sudah berjalan dengan baik
[oracle@test42 ~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.1.0 – Production on 31-JUL-2012 09:10:33
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test42.localdomain)(PORT=1521)))
STATUS of the LISTENER
————————
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.1.0 – Production
Start Date                18-APR-2012 09:02:18
Uptime                    104 days 0 hr. 8 min. 15 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/test42/listener/alert/log.xml
Listening Endpoints Summary…
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=test42.localdomain)(PORT=1521)))
Services Summary…
Service “test42” has 1 instance(s).
Instance “test42”, status READY, has 1 handler(s) for this service…
Service “test42XDB” has 1 instance(s).
Instance “test42”, status READY, has 1 handler(s) for this service…
The command completed successfully

d. tnsnames.ora sudah terkonfigurasi
[oracle@test42 ~]$ cd /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/
[oracle@test42 admin]$ cat tnsnames.ora
TEST42 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.20.33.42)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = test42)
)
)
========================================================================

Langkah – langkahnya
1. pastikan php sudah terinstall, minimal mirip seperti ini
# rpm -qa | grep php
php-pdo-5.1.6-23.el5
php-cli-5.1.6-23.el5
php-ldap-5.1.6-23.el5
php-5.1.6-23.el5
php-pear-1.4.9-4.el5.1
php-common-5.1.6-23.el5
php-mysql-5.1.6-23.el5
php-devel-5.1.6-23.el5

2. download oci8 , yang terbaru dan install
# tar -xvzf oci8-1.4.7.tgz
# cd oci8-1.4.7
# phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20050922
Zend Extension Api No:   220051025

# ./configure -with-oci8=shared,/u01/app/oracle/product/11.2.0/dbhome_1
# make
…………………….
…………………….
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

# make install
Installing shared extensions:     /usr/lib64/php/modules/

3. edit /etc/php.ini, tambahkan add extension=oci8.so, kemudian lakukan restart server
# vi /etc/php.ini
# service httpd restart

4. edit /etc/sysconfig/httpd, tambahkan, kemudian lakukan restart server

# vi /etc/sysconfig/httpd
export ORACLE_SID=TEST42
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export LD_LIBRARY_PATH=/u01/app/oracle/product/11.2.0/dbhome_1/lib

5. membuat php : phpinfo.php
# mkdir -p /var/www/html/php
# cd /var/www/html/php
# vi phpinfo.php
<?php
phpinfo();
?>

6. test di browser — http://172.20.33.42/php/phpinfo.php
OCI8 Support                             enabled
Version                                 1.4.7
Revision                                 $Revision: 321634 $
Active Persistent Connections             0
Active Connections                         0
Oracle Run-time Client Library Version     11.2.0.1.0
Oracle Version                             11.2
Compile-time ORACLE_HOME                 /u01/app/oracle/product/11.2.0/dbhome_1
Libraries Used                             -Wl,-rpath,/u01/app/oracle/product/11.2.0/dbhome_1/lib -L/u01/app/oracle/product/11.2.0/dbhome_1/lib -lclntsh
Temporary Lob support                     enabled
Collections support                     enabled

7. membuat php : env.php dan test di browser — http://172.20.33.42/php/env.php
<?php
echo $_ENV[‘ORACLE_HOME’] . “\n”;
echo $_ENV[‘LD_LIBRARY_PATH’] . “\n”;
echo $_ENV[‘PATH’] . “\n”;
?>

8. membuat php : oracle01.php dan test dengan browser
<?php
echo “ORACLE_HOME=”.getenv(“ORACLE_HOME”);
echo “<br />\n”;

$ora_conn = oci_connect(“SCOTT”,”TIGER”,”TEST42″);
if (!$ora_conn)
{
$ora_conn_erno = oci_error();
echo ($ora_conn_erno[‘message’].”\n”);
oci_close($ora_conn);
}
else
{
echo “Connection Succesful\n”;
oci_close($ora_conn);
}
?>

9. membuat php : oracle02.php dan test dengan browser
<?php
$ora_conn = oci_connect(“SCOTT”,”TIGER”,”TEST42″);

if (!$ora_conn)
{
$err = oci_error();
echo “Oracle Connect Error ” . $err[‘text’];
}
else
{
echo “Successfully connected to Oracle.”;
$stid = oci_parse($ora_conn, ‘SELECT * FROM DEPT’);
oci_execute($stid);
echo “<table border=’1′>\n”;
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
echo “<tr>\n”;
foreach ($row as $item)
{
echo ”    <td>” . ($item !== null ? htmlentities($item, ENT_QUOTES) : “&nbsp;”) . “</td>\n”;
}
echo “</tr>\n”;
}
echo “</table>\n”;
oci_close($ora_conn);
}
?>

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: