Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect an Oracle database from PHP

Tags:

php

oracle

How do I connect to an Oracle database from PHP?

like image 217
Tharindu Avatar asked May 10 '11 09:05

Tharindu


People also ask

Can we connect Oracle Database with PHP?

Connecting to Oracle from PHP using ODBC Driver for Oracle PHP is one of the most popular programming languages for website development. ODBC drivers are connectors that make PHP development database agnostic — your software written in PHP will function with any vendor's database management system.

How do I connect to a database in Oracle?

On the Home page, click Create, then click Connection. In Create Connection dialog, click the connection type, for example, Oracle Database.

Can I connect to Oracle Database with MySQL?

You can use the SQL Gateway from the ODBC Driver for Oracle to query Oracle data through a MySQL interface.


2 Answers

Forth link in google after searching for your exact questions brought up the following link: http://me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/

<?php
    $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;

    if($c = OCILogon("system", "your database password", $db))
    {
        echo "Successfully connected to Oracle.\n";
        OCILogoff($c);
    }
    else
    {
        $err = OCIError();
        echo "Connection failed." . $err[text];
    }
?>
like image 106
RobertPitt Avatar answered Nov 02 '22 23:11

RobertPitt


I assumed you want to connect php with oracle databases. so, I give you two of file, this is represent a basic php-oracle for your reference. good luck!

form.php

<form name="form1" method="post" action="login.php">
  <label> User Name
  <input type="text" name="nis" id="nis">
  </label>
  <label> Password
  <input type="password" name="password" id="password">
  </label>
  <label>
  <input type="submit" name="submit" id="button" value="Login">
  </label>
</form>

login.php

<?php

//create table users (userid varchar2(10), password varchar2(20), constraint pk_users primary key (userid));
//insert into users values('kharis', 'pass123');

$nis = isset($_POST['nis']) == true ? $_POST['nis'] : '';
$password= isset($_POST['password']) == true ? $_POST['password'] : '';

if(empty($nis) or empty($password)){
    echo "UserID atau Password kosong";}
else
{
    $db = "(DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = patronus.ad-ins.com)(PORT = 1521))
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = XE)
        )
      )" ;
    $connect = oci_connect("HR", "hr", "XE");
    $query = "SELECT * from users WHERE userid='".$nis."' and password='".$password."'";
    $result = oci_parse($connect, $query);
    oci_execute($result);
    $tmpcount = oci_fetch($result);
    if ($tmpcount==1) {
        echo "Login Success";}
    else
    {
        echo "Login Failed";
    }
}
?>
like image 4
Kharis Avatar answered Nov 02 '22 22:11

Kharis