# Working with Oracle Database using PHP
## Installation
### Ubuntu 22.04
#### Installing php
```shell
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
```shell
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
```shell
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)
```shell
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
```shell
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)
```shell
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
```shell
# 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=
<?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) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
```
The execution is done in the following way:
```shell
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
- Ubuntu 22.04
- [How to Install PHP 8 on Ubuntu 22.04](https://linuxhint.com/install-php-8-ubuntu-22-04/)
- [How to install OCI8 on Ubuntu 18.04 and PHP 7.2](https://gist.github.com/Yukibashiri/cebaeaccbe531665a5704b1b34a3498e)
- [Install OCI8 on Ubuntu 20.04 with PHP 7.4](https://gist.github.com/eSkiSo/781269c79b4dd740e90fcc059c1985ae)
- Oracle Linux 8
- [PHP Packages for Oracle Linux](https://yum.oracle.com/oracle-linux-php.html)
- [How To Install PHP 8.1 on AlmaLinux 8|Oracle Linux 8](https://computingforgeeks.com/how-to-install-php-on-almalinux-oracle-linux/)
- [How to install Oracle Client Instant (OCI8) on Linux](https://vfac.fr/blog/how-to-install-oracle-client-instant-oci8-on-linux)
- CentOS 7
- [How To Install PHP 7, 7.2 & 7.3 On CentOS 7](https://phoenixnap.com/kb/install-php-7-on-centos)
- [How to install PHP 7.2 on CentOS 7/RHEL 7](https://www.cyberciti.biz/faq/how-to-install-php-7-2-on-centos-7-rhel-7/)
- [How to Install PHP on CentOS](https://linuxstans.com/how-to-install-php-centos/)
- [How to Install PHP 5.6 on CentOS 7](https://www.tecmint.com/install-php-5-6-on-centos-7/)
- [Installing OCI8 & PHP 7.2 on RHEL or CentOS 6](https://alvinbunk.wordpress.com/2018/02/19/installing-oci8-php-7-2-on-rhel-or-centos-6/)
- [Installation of Oracle extensions for PHP](https://blog.remirepo.net/post/2020/05/18/Installation-of-Oracle-extensions-for-PHP)