Connect PHP 7 to Oracle Database using packages from Oracle Linux Yum Server

We recently added PHP 7.4 to our repos on Oracle Linux yum server. These repos include also include the PHP OCI8 extenstion to connect your PHP applications to Oracle database.
In this post I describe the steps to install PHP 7.4, PHP OCI8 and Oracle Instant Client on Oracle Linux to connect PHP to Oracle Database. For this blog post, I used a free Autonomous Database included in Oracle Cloud Free Tier.

Install Oracle Instant Client

Oracle Instant Client RPMs are available on Oracle Linux yum server also. To access them, install the oracle-release-el7 package first to setup the appropriate repositories:

$ sudo yum -y install oracle-release-el7
$ sudo yum -y install oracle-instantclient19.5-basic
If you want to be able to use SQL*Plus (this can come in handy for some sanity checks), install the SQL*Plus RPM also:
$ sudo yum -y install oracle-instantclient19.5-sqlplus

Create a Schema and Install the HR Sample Objects (Optional)

You can use any schema you already have in your database. I’m going to use the HR schema from the Oracle Database Sample Schemas on github.com If you already have a schema with database objects to work with, you can skip this step.
$ yum -y install git
$ git clone https://github.com/oracle/db-sample-schemas.git
$ cd db-sample-schemas/human_resources
As SYSTEM (or ADMIN, if you are using Autonomous Database), create a user PHPTEST
SQL> grant connect, resource, create view to phptest identified by <YOUR DATABASE PASSWORD>;
SQL> alter user PHPTEST quota 5m on USERS;
If you are using Autonomous Database like I am, change the tablespace above to DATA:
SQL> alter user phptest quota 5m on DATA;
As the PHPTEST user, run the scripts hr_cre.sql and hr_popul.sql to create and populate the HR database objects
SQL> connect phptest/<YOUR DATABASE PASSWORD>@<YOUR CONNECT STRING>
SQL> @hr_cre.sql
SQL> @hr_popul.sql

Install PHP and PHP OCI8

To install PHP 7.4, make sure you have the latest oracle-php-release-el7 package installed first.
$ sudo yum install -y oracle-php-release-el7
Next, install PHP and the PHP OCI8 extenstion corresponding to the Oracle Instant Client installed earlier:
$ sudo yum -y install php php-oci8-19c
Running the following php code snippet should verify that we can connect PHP to the database and bring back data. Make sure you replace the schema and connect string as appropriate.
<!--?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $conn = oci_connect('phptest', '<YOUR DATABASE PASSWORD>', '<YOUR CONNECT STRING>');
    $stid = oci_parse($conn, 'SELECT last_name FROM employees');
    oci_execute($stid);
    echo "\n";
    while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
      foreach ($row as $item) {
        echo $item ."\n";
       }
    }
?-->
Create a file emp.php based on the code above.

Run it!

$ php emp.php
This should produce the following:
King
Kochhar
De Haan
Hunold
Ernst
Austin
Pataballa
Lorentz
Greenberg
Faviet
Chen
Sciarra
...
sudo setsebool -P httpd_can_network_connect on
sudo systemctl stop httpd
sudo systemctl start httpd

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud