Connect PHP and Oracle database 10g

I posted before how to create APIs (Web Services) from Database 10g using PLSQL Gateway in this post.

When Android developer reads API , Arabic fonts was not complete when using FLUTTER, but when he using Android Studio he reads without any losing characters.

I found using php server , it works fine.

Here are steps to create your first APIs to connect PHP and ORACLE 10g together:
1#  HR schema is used in the following example
2#  Download this Github repository from  here (It includes AppServ and API Folders) APPSERV another link
3# Install AppServ to C:\AppServ
4#Extract ora_php-master.zip to C:\AppServ\www
5# Rename C:\AppServ\www\ora_php-master to  C:\AppServ\www\api
 Now your folders and files  should looks like this:

6# Config Folder: Contains database.php ,

<?php

class Database{

 // specify your own database credentials

    private $host     = "localhost"; //DataBase Server

    private $db_name  = "PROD";    //DataBase Name

    private $username = "hr";            //User Name, which We'll Create API for

    private $password = "hr";            //User Password

    public  $conn;                              //Public Varaible
// get the database connection
    public function getConnection(){
  $this->conn = null;
  try{
   $this->conn = oci_connect($this->username, 
                             $this->password, 
           '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)
                                      (HOST = '.$this->host.')
                 (PORT = 1521)) 
               (CONNECT_DATA = (SERVICE_NAME = '.$this->db_name.') 
                               (SID = '.$this->db_name.')))' , 'AL32UTF8');
   }
  catch(Exception $exception){
   echo "Connection error: " . $exception->getMessage();
   }
   return $this->conn;
   }

7# Objects Folder : Contains all Users, if  we do have other users .


8# User Folder : For every object we create folder contains all types of API  (GET, PUT, POST....)


9# To Test API : goto http://localhost/api/user/read.php
URL consists of : Machine_Ip/API_Folder(Which was ora_php-master)/APIS_FOLDER(USER)/API_METHOD(read.php or search.php?id=107)

10# Done , Now you can connect to database from any machine, Android, iOS etc......

UPDATE #1
You may face an error  like Call to undefined function oci_connect .

Here's how to fix it :
# You need to enable OCI for php
# Edit php.ini

# enable extension=php_oci8.dll
BEFORE : ;extension=php_oci8.dll
AFTER    : extension=php_oci8.dll

# Restart AppServ .

UPDATE #2

To make sure OCI works , Open browser and goto  http://localhost/phpinfo.php
it must looks have oci like that :




UPDATE #3

Connecting to remote DataBase is some tricky , You just add connection string  when calling OCI_CONNECT , here's a full example :


<?php
//Connect String
$db="(DESCRIPTION=
     (ADDRESS_LIST=
       (ADDRESS=(PROTOCOL=TCP)
         (HOST=192.168.1.25)(PORT=1521)
       )
    )
        (CONNECT_DATA=(SID=HR))
 )";

//Connect to DB

$conn = oci_connect('hr','hr',$db); 


//Run a sample query

$qry = oci_parse($conn, 'select SYSDATE from DUAL');


if (!qry){

    echo "Not connected";

}else{

    echo "Connected";

}

UPDATE #4

If you still cannot see OCI8 in http://localhost/phpinfo.php , here's final steps :

1# download  oracle instant client (instantclient-basic-win32-10.2.0.5) from here 

2# Extract all contents to the zipped file to :
   - C:\AppServ\Apache2.2\bin
  and
  - C:\AppServ\php5\ext

3#  Edit System Variables PATH 
  add C:\AppServ\php5\ext  at the end of existing text

4# Restart the Apache server

5# You are done !

No comments:

Post a Comment

Remove unused Layouts in Oracle Apex

 Tables used : APEX_XXXXXXX.WWV_FLOW_REPORT_LAYOUTS APEX_XXXXXXX.WWV_FLOW_SHARED_QUERIES use the following query to delete unused Layouts. ...