Try   HackMD

Working with Oracle Database using PHP

Installation

Ubuntu 22.04

Installing php

sudo apt update
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y

sudo add-apt-repository ppa:ondrej/php # if error -> ignore
sudo apt install php8.1 -y
# verify
php -v

Installing oci8 (Oracle Instance Client for PHP)

Download Oracle Instant Client and SDK from Oracle: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

Download files:

  • Basic Package (ZIP): instantclient-basic-linux.x64-21.XXX.zip
  • SDK Package (ZIP): instantclient-sdk-linux.x64-21.XXX.zip
sudo mkdir -p /usr/lib/oracle/21.1/client64
sudo cp ~/Downloads/instantclient-*.zip /usr/lib/oracle/21.1/client64/

cd /usr/lib/oracle/21.1/client64

sudo unzip instantclient-basic-linux.x64-21.*.zip
sudo unzip instantclient-sdk-linux.x64-21.1.*.zip

sudo mv instantclient_21_1 lib

# create symbolic link to the new Instant Client files
cd ./lib/
sudo ln -s libclntsh.so.21.1 libclntsh.so # It may already exist, continue
sudo ln -s libocci.so.21.1 libocci.so # It may already exist, continue

# or white manually
sudo echo /usr/lib/oracle/21.1/client64/lib > /etc/ld.so.conf.d/oracle.conf

sudo ldconfig

# install php-dev php-pear build-essential and libaio1
sudo apt-get install php-dev php-pear build-essential libaio1

sudo pecl channel-update pecl.php.net
sudo pecl install oci8
# during install it will require the path to Instant Client, write this:
# instantclient,/usr/lib/oracle/21.1/client64/lib

# load OCI8 into PHP
# or white manually
sudo echo "extension=oci8.so" >> /etc/php/8.1/cli/php.ini

# check if OCI is loaded in PHP
php -i | grep oci

Oracle Linux 8

Installing php

sudo dnf install epel-release
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

sudo dnf update

sudo dnf module list reset php
sudo dnf module enable php:remi-8.1
sudo dnf install php
# verify
php -v

Installing oci8 (Oracle Instance Client for PHP)

sudo dnf install oraclelinux-developer-release-el8 oracle-instantclient-release-el8
sudo dnf module enable php:remi-8.1 php-oci8
sudo yum install php-oci8<instant client release>
# check if OCI is loaded in PHP
php -i | grep oci

CentOS 7

Installing php

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install yum-utils

sudo yum-config-manager --enable remi-php56   # install PHP 5.6
sudo yum-config-manager --enable remi-php55   # install PHP 5.5
sudo yum-config-manager --enable remi-php72   # install PHP 7.2

sudo yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
# verify
php -v

Installing oci8 (Oracle Instance Client for PHP)

sudo yum install php php-pecl-mcrypt php-cli php-gd php-curl php-mysqlnd php-ldap php-zip php-fileinfo php-xml php-intl php-mbstring php-opcache php-process systemtap-sdt-devel php-pear php-json php-devel php-common php-bcmath php-pdo

sudo yum install php-oci8
# check if OCI is loaded in PHP
php -i | grep oci

Testing

# create test directory
mkdir oracle-db-test
cd oracle-db-test
touch ./index.php

# load OCI8 into PHP
# or white manually
sudo echo "extension=oci8.so" >> /etc/php.ini

sudo yum install oracle-instantclient-basic-21.3.0.0.0-1.x86_64.rpm
sudo yum install oracle-instantclient-devel-21.3.0.0.0-1.x86_64.rpm

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

Write the following code in the index.php file

<?php // fill in the contents of the following variables correctly $usr = 'username'; $pswd = 'password'; $connStr = '//hostname:port/service_name'; $conn = oci_connect($usr, $pswd, $connStr, 'AL32UTF8'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } // fill in the correct SQL query $queryStr = 'SELECT * FROM employees'; $stid = oci_parse($conn, $queryStr); oci_execute($stid); echo "<table border='1'>\n"; while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) { echo "<tr>\n"; foreach ($row as $item) { echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; ?>

The execution is done in the following way:

cd oracle-db-test
php -S 127.0.0.1:8000 # or using any other available IP/port of the machine
# visit http://127.0.0.1:8000

Resources