X

News, tips, partners, and perspectives for the Oracle Linux operating system and upstream Linux kernel work

  • February 7, 2018

Connect PHP 7.2 to Oracle Database 12c using Oracle Linux Yum Server

A few weeks ago we added dedicated repositories for PHP to the Oracle Linux yum server. And in the last few days, we've added PHP 7.2 also. Our repos include php-oci8 as well so that you can connect your PHP applications to the Oracle database.

In this post I describe the steps to install PHP 7.2, php-oci8 and the Oracle Instant Client to connect PHP to the Oracle Database. I'll be the the first to admit that I don't know anything about PHP, but I was able to get the following to work.

Download and Install Instant Client

Download the Instant Client Package - Basic (oracle-instantclient12.2-basic) RPM as well as the  Instant Client Package - SQL*Plus (oracle-instantclient12.2-sqlplus) RPM from the Instant Client Downloads for Linux x86-64 on OTN

Install the instant client RPM using yum. Position yourself in the directory where you downloaded the RPM. 

$ sudo yum -y install oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm

If you want to be able to use SQL*Plus (which can be handy for some sanity checks), install the SQL*Plus RPM also:

$ sudo yum -y install oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm

Add the Oracle Instant Client to the runtime link path.

$ sudo sh -c "echo /usr/lib/oracle/12.2/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf" $ sudo ldconfig

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 the SYSTEM user, create a user PHPTEST

SQL> grant connect, resource, create view to phptest identified by phptest; SQL> alter user phptest quota 5m on users;

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/phptest@10.0.1.127/orclpdb1
SQL> @hr_cre.sql
SQL> @hr_popul.sql 

Install PHP and PHP OCI8

To install PHP 7.2, make sure you have the latest Oracle Linux yum server repo file.

As root:

(root) # cd /etc/yum.repos.d/ (root) # mv public-yum-ol7.repo public-yum-ol7.repo.bak (root) # wget -O ol7-yum.repo http://yum.oracle.com/public-yum-ol7.repo

Enable the PHP 7.2 repository:

$ sudo yum -y install yum-utils $ sudo yum-config-manager --enable ol7_developer_php72

Install php and php-oci8-12c

$ sudo yum -y install php php-oci8-12c

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', 'phptest', '10.0.1.127/orclpdb1'); $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
...

Join the discussion

Comments ( 2 )
  • Yogesh Chaudhari Wednesday, September 12, 2018
    I don't know much about PHP, but I am able to follow your blog and able to access Oracle database from PHP code. Thanks!
  • Andrey Prozorov Monday, November 12, 2018
    Man!! you are a god!!! 4 days i have killed to run oci8 under php. and 5 minutes by your article. In whole internet any manuals missed `ldconfig` part. THANK YOU!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha