Install PHP OCI8 extension to connect Oracle with PHP

Documentasi ini saya tulis sebagai referensi pribadi saya dan semoga bermanfaat juga untuk teman pembaca semua.

Sebelumnya kita mesti memahami Ekstensi dalam PHP untuk Mengakses Database Oracle, untuk mendukung akses database Oracle dari lingkungan PHP, PHP menyediakan dukungan terhadap beberapa ekstensi berikut :

  • Oracle: Ekstensi ini dirancang untuk para pengembang web yang databasenya masih menggunakan oracle 7.
  • ODBC: Ekstensi ini dapat digunakan sebagai pilihan lain utnuk membangun koneksi antara PHP dan Oracle
  • PDO: Ekstensi ini merupakan Application Programing Interface (API) yang menawarkan kemudahan akses database antara PHP dan Oracle. Dengan menggunakan ekstensi ini, kode akan tampak simple (sederhana)
  • OCI8: Ekstensi ini mendukung paling banyak fungsi yang terdapat didalam Oracle Call Interface (OCI)

Disini yang saya gunakan untuk akses database antara PHP dan Oracle Menggunakan ekstensi OCI8.

OCI8 is an extension for providing APIs to Oracle database management system. However, installing OCI8 is not just easy like apt-get since it requires SDK offered by Oracle and its distribution license prohibits the inclusion to linux distribution. That means we have to download and compile it by ourselves. Fortunately, it is a little bit easy for Ubuntu.
1. Download Prerequisite Debian Packages

# Get libaio1 - Asynchronous IO used by databases and
# needed by Oracle.
> apt-get install libaio1
# Get alien (for converting rpm to deb)
> apt-get install alien

2. Download Oracle RPM packages Oracle Instant Client (basic dan devel) dari : http://www.oracle.com/technetwork/topics/linuxsoft-082809.html

> oracle-instantclient11.2-basiclite-11.2.0.3.0-1.i386.rpm
> oracle-instantclient11.2-devel-11.2.0.3.0-1.i386.rpm

3. Convert RPM packages to Debian package

> alien oracle-instantclient11.2-basiclite-11.2.0.3.0-1.i386.rpm
> alien oracle-instantclient11.2-devel-11.2.0.3.0-1.i386.rpm

4.Install Debian packages

> dpkg -i oracle-instantclient11.2-basiclite-11.2.0.3.0-1.i386.deb
> dpkg -i oracle-instantclient11.2-devel-11.2.0.3.0-1.i386.deb

5. Configure OS to search Oracle libraries by running the following command

> echo /usr/lib/oracle/11.2/client/lib >> /etc/ld.so.conf.d/oracle.conf
> ldconfig

6. Install needed PHP packages

> apt-get install php5-dev php-pear

7. Now, we are ready to install the PHP OCI8 extension. Download oci8 package from : http://pecl.php.net/package/oci8, Run the following commands to install PHP OCI8 extension.

> pecl install oci8-1.4.9.tgz
# Enter ORACLE HOME ...
> instantclient,/usr/lib/oracle/11.2/client/lib

8. Configure file /etc/php5/apache2/php.ini Add the line extension=oci8.so after [OCI8], for example,

[OCI8]
extension=oci8.so

9. Configure file /etc/php5/cli/php.ini Configure the same way as /etc/php5/apache2/php.ini above.
10. Restart apache2

> /etc/init.d/apache2 restart

11. To confirm the extension is configured Check phpinfo, Now It should show that the oci8 support is enabled.

> php -i |grep -i "OCI8 Support" -A20

12. Or Open a web browser and navigate to the phpinfo file ( in my case it is http://localhost/phpinfo )

#phpinfo.php
phpinfo();
?>

Load the script into a browser using a URL similar to "http://localhost/phpinfo.php". The browser page should contain an "oci8" section saying "OCI8 Support enabled".

13. Cek connection, Here is a simple PHP script: oci-test.php

<!--?php &lt;br ?--> // USUARIO, SENHA, HOST
$c = oci_connect ("franzlpstersz", "milan", "localhost/XE");
if (!$c) {
echo "Connect to Oracle Database ERROR";
exit();
}
$query= "SELECT BUKU_ISBN, BUKU_JUDUL, BUKU_JMLHALAMAN FROM BUKU ORDER BY BUKU_ISBN";
$statemen = oci_parse ($c, $query);
oci_execute ($statemen, OCI_DEFAULT);
echo "

Conecao Succeso - Go to Query : $query

";
echo "

Resultado query:

";
while ($baris = oci_fetch_array ($statemen, OCI_BOTH)) {
echo $baris ['BUKU_ISBN'].
" - ".
$baris ['BUKU_JUDUL'].
" - ".
$baris['BUKU_JMLHALAMAN'].
"
";
}
oci_free_statement($statemen);
oci_close($c);
?>

 

Load the script into a browser using a URL similar to (in my case it is "http://localhost/oracle/oci-test.php)".

Facebook Comments

Post a Comment

Your email address will not be published. Required fields are marked *

© Franz East 2009 - 2018. | Powered by Coffee + Cigarete & Site Hosted By IDwebhost.