Cara Install Ekstensi PHP Oracle di XAMPP Windows
Tidak dapat dipungkiri, Oracle DB sampai saat ini masih menjadi salah satu solusi database enterprise. Untuk developer pemula, khususnya yang masih menggunakan XAMPP dan Windows, tutorial berikut mungkin berguna bagi kamu sekalian yang ingin menginstall Ekstensi Oracle DB untuk PHP.
Untuk memastikan tidak salah download ekstensi PHP yang cocok, silakan jalankan XAMPP dulu. Dan buka halaman dashboard XAMPP/Versi PHP. Perhatikan item dalam kotak merah berikut:
Jadi, arsitektur komputer yang saya contohkan adalah X86 dengan Thread Safety. Maka, cari dan download ekstensi PHP juga yang x86 dan enabled TS (Thread Safety).
Download ekstensi Oracle DB untuk PHP x86 dari PECL https://pecl.php.net/package/oci8. Pilih yang sesuai dengan konfigurasi komputer dan versi PHP yang dipakai.
Kemudian ekstrak file DLL hasil download dari link diatas ke folder ekstensi PHP, misalnya D:\xampp\php\ext. Kita akan menggunakan file php_oci8_12c.dll yang baru dari folder ekstensi tersebut. Jadi edit dulu file php.ini yang digunakan XAMPP. Biasanya letaknya di D:\xampp\php\php.ini. Edit saja dengan notepad
Kemudian tulis:
1
| extension=oci8_12c |
sehingga ekstensi PHP oci8_12c dipakai oleh XAMPP.
Langkah Kedua
Untuk memastikan PHP bisa berkomunikasi dengan database Oracle, silakan install Basic Client Package Oracle yang bisa didownload dari sini: http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
Ekstrak file dari hasil download tersebut, misalnya hasilnya jadi folder di D:\oracle\instantclient_12_2.
Setelah itu tambah entri PATH di sistem windows, caranya klik kanan My Computer di Explorer. Pilih System -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables. Pilih PATH. Tambahkan path ke folder instant_client diatas.
Sampai dilangkah ini, restart XAMPP dan buka kembali versi PHP. Harusnya Oracle DB sudah ada di salah satu entri phpinfo.
Fahimtum?
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
Installing XAMPP on Windows for PHP and Oracle Database
Today's guest post comes from Tianfang Yang who's been working with the Oracle Database extensions for PHP.
This post shows how to install XAMPP on Windows to run PHP applications that connect to a remote Oracle Database.
XAMPP is an open source package that contains Apache, PHP and many PHP 'extensions'. One of these extension is PHP OCI8 which connects to Oracle Database.
To install XAMPP:
- Download "XAMPP for Windows" and follow the installer wizard. I installed into my D: drive.
- Start the Apache server via the XAMPP control panel.
- Visit http://localhost/dashboard/phpinfo.php via your browser to see the architecture and thread safety mode of the installed PHP. Please note this is the architecture of the installed PHP and not the architecture of your machine. It’s possible to run a x86 PHP on an x64 machine.
- [Optional] Oracle OCI8 is pre-installed in XAMPP but if you need a newer version you can download an updated OCI8 PECL package from pecl.php.net. Pick an OCI8 release and select the DLL according to the architecture and thread safety mode. For example, if PHP is x86 and thread safety enabled, download "7.2 Thread Safe (TS) x86". Then replace "D:\xampp\php\ext\php_oci8_12c.dll" with the new "php_oci8_12c.dll" from the OCI8 PECL package.
- Edit "D:\xampp\php\php.ini" and uncomment the line "extension=oci8_12c". Make sure "extension_dir" is set to the directory containing the PHP extension DLLs. For example,
extension=oci8_12c
extension_dir="D:\xampp\php\ext"
- Download the Oracle Instant Client Basic package from OTN.Select the correct architecture to align with PHP's. For Windows x86 download "instantclient-basic-nt-12.2.0.1.0.zip" from the Windows 32-bit page.
- Extract the file in a directory such as "D:\Oracle". A subdirectory "D:\Oracle\instantclient_12_2" will be created.Add this subdirectory to the PATH environment variable. You can update PATH in Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables -> PATH. In my example I set it to "D:\Oracle\instantclient_12_2".
- Restart the Apache server and check the phpinfo.php page again. It shows the OCI8 extension is loaded successfully.If you also run PHP from a terminal window, make sure to close and reopen the terminal to get the updated PATH value.
- To run your first OCI8 application, create a new file in the XAMPP document root "D:\xampp\htdocs\test.php". It should contain:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
<?php
error_reporting
(E_ALL);
ini_set
(
'display_errors'
,
'On'
);
$username
=
"hr"
;
// Use your username
$password
=
"welcome"
;
// and your password
$database
=
"localhost/orclpdb"
;
// and the connect string to connect to your database
$query
=
"select * from dual"
;
$c
= oci_connect(
$username
,
$password
,
$database
);
if
(!
$c
) {
$m
= oci_error();
trigger_error(
'Could not connect to database: '
.
$m
[
'message'
], E_USER_ERROR);
}
$s
= oci_parse(
$c
,
$query
);
if
(!
$s
) {
$m
= oci_error(
$c
);
trigger_error(
'Could not parse statement: '
.
$m
[
'message'
], E_USER_ERROR);
}
$r
= oci_execute(
$s
);
if
(!
$r
) {
$m
= oci_error(
$s
);
trigger_error(
'Could not execute statement: '
.
$m
[
'message'
], E_USER_ERROR);
}
echo
"<table border='1'>\n"
;
$ncols
= oci_num_fields(
$s
);
echo
"<tr>\n"
;
for
(
$i
= 1;
$i
<=
$ncols
; ++
$i
) {
$colname
= oci_field_name(
$s
,
$i
);
echo
" <th><b>"
.htmlspecialchars(
$colname
,ENT_QUOTES|ENT_SUBSTITUTE).
"</b></th>\n"
;
}
echo
"</tr>\n"
;
while
((
$row
= oci_fetch_array(
$s
, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo
"<tr>\n"
;
foreach
(
$row
as
$item
) {
echo
"<td>"
;
echo
$item
!==null?htmlspecialchars(
$item
, ENT_QUOTES|ENT_SUBSTITUTE):
" "
;
echo
"</td>\n"
;
}
echo
"</tr>\n"
;
}
echo
"</table>\n"
;
?>
You need to edit this file and set your database username, password and connect string. If you are using Oracle Database XE, then the connect string should be "localhost/XE".The SQL query can also be changed. Currently it queries the special DUAL table, which every user has. - Load the test program in a browser using http://localhost/test.php. The output will be the single value "X" in the column called "DUMMY".
You can read more about PHP OCI8 in the PHP manual, and in the free Underground PHP and Oracle Manual from Oracle.
Enjoy your coding with OCI8!
Tidak ada komentar:
Posting Komentar