[PHP] Connect Oracle Database


Deskripsi
     Tutorial mengenai koneksi php ke oracle database menggunakan OCI.

Howto
1. You must have OCI Extention, if you don't have you can Download Here
2. Extract Extention in PHP5.x.x/Ext/[Extract Here]
3. Find Php.ini and add the extention, if you don't know how to add extention on php , find on google first.
4. Download and install Oracle Instant Client
5. Download and install Oracle Client Here
6. Create file TNSname.ora at Admin Folder in your Oracle Client, and add this line
// Based on you setup, it's only sample
DB_NAME =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS =
        (PROTOCOL = TCP)
        (HOST = XX.XX.XX.XX)
        (PORT = 1526)
      )
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ServiceOrSID)
    )
  )

7. if all setup complete, now you can connect to oracle database from php with this code
<?php
$host = 'XX.YY.ZZ.GG';
$port = 1526;
$database_name = 'test';
$username = 'user';
$password = 'test';
$SIDorServiceName = 'myservice';

$TNSEditor = "(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = $host)(PORT = $port))
(CONNECT_DATA = (SID = $SIDorServiceName)))";

$statusOra = oci_connect("$username", "$password", $TNSEditor);

if($statusOra)
{
echo "connect sukses"."<br>";
$stid = oci_parse($statusOra, 'SELECT * FROM table where rownum <= 1');
oci_execute($stid);

while ($row = oci_fetch_array($stid,OCI_BOTH))
{
//echo $row["TASK_NAME"]."<br>";
  //echoing your field here
}
}
else
{
echo "connect failed";
}
oci_close($statusOra);
?>

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih