Build a PHP Application

To build a PHP application that accesses an Autonomous Database, you start by configuring your development system to support database access that can take advantage of the continuous availability and high performance features of Autonomous Database.

After configuring your development system, you code database connections and SQL statements in your application to take advantage of the continuous availability and high performance features.

Configure Your PHP Development System

To configure your development system so that your PHP application can take advantage of the continuous availability and high performance features of an Autonomous Database, you perform these steps.

  1. Download and install PHP.
  2. Download and install Oracle Instant Client.
  3. Download and install PHP OCI8.
  4. Download the client credentials for the database and make them available to Oracle Instant Client.

Before You Begin

Your development system must meet certain criteria to configure it successfully.

Download and Install PHP

Download and install PHP for your system's OS and architecture:

  • Oracle Linux:

    Run these commands to download and install PHP and to download and install PEAR to get PHP's pecl package management command:

    sudo dnf install -y oracle-release-el8 oracle-php-release-el8
    sudo dnf install -y php php-devel php-xml dtrace-utils
    wget http://pear.php.net/go-pear.phar
    sudo php go-pear.phar
  • Other OSes and architectures:

    Go to the PHP Installation and Configuration page, and follow the instructions for your system's OS and architecture.

Download and Install Oracle Instant Client

Download and install the Oracle Instant Client basic package for your system's OS and architecture:

  • Oracle Linux:

    Run these commands to download and install the Oracle Instant Client basic package:

    sudo dnf -y install oracle-release-el8
    sudo dnf -y install oracle-instantclient21.13-basic oracle-instantclient21.13-devel

    (If you want to see a list of all Instant Client packages, go to http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/index.html.)

  • Other OSes and architectures:

    1. Go to the Oracle Instant Client Downloads page and select the download for your system's OS and architecture.

    2. On the download page, accept the Oracle Technology Network License Agreement, download the latest versions of the Basic Package and the SDK Package, and then install them by following the instructions at the bottom of the download page.

Download and Install PHP OCI8

Download and install the OCI8 extension for PHP for your system's OS and architecture:

  • Oracle Linux:

    Run these commands to download and install the latest version of the OCI8 extension:

    sudo PHP_DTRACE=yes pecl install oci8
    sudo sh -c "echo extension=oci8.so > /etc/php.d/20-oci8.ini"
    sudo sh -c "echo oci8.events = On > /etc/php.d/20-oci8.ini"

    If your applications needs Oracle environment variables, add them to the /etc/sysconfig/httpd config file, for example:

    NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
    ORA_SDTZ=UTC

    If you add such variables, restart the Apache HTTP server to read the new variables:

    sudo systemctl restart httpd
  • Other OSes and architectures:

    1. Run this command

      pecl install oci8
    2. Add the following lines to your PHP installation's php.ini file:

      extension=oci8.so
      oci8.events = On

Download and Install Client Credentials for the Database

  1. Download the zip file containing client credentials for your database to a secure directory on your computer.

    This zip file is available for download from the database's Details page in the Oracle Cloud console. Download the credentials as follows.

    1. In your web browser, sign in to Oracle Cloud and navigate to the Details page for the Autonomous Database.

    2. Click DB Connection.

    3. On the Database Connection page click Download.

    4. In the Download Wallet dialog, enter a wallet password in the Password field and confirm the password in the Confirm Password field.

      The password must be at least 8 characters long and must include at least 1 letter and either 1 numeric character or 1 special character.

    5. Click Download to save the client credentials zip file to a secure directory.

  2. After downloading the zip file, follow these steps:

    1. Unzip the client credentials zip file.

    2. Edit the sqlnet.ora file provided in the client credentials, replacing "?/network/admin" with the full path of the directory where you unzipped the client credentials; for example, change:

      (DIRECTORY="?/network/admin")

      to:

      (DIRECTORY="/users/jdoe/adbcredentials")
    3. Create the TNS_ADMIN environment variable, setting its value to the full path of the directory where you unzipped the client credentials.

Code Database Connections and SQL Statements

After configuring your development system to support PHP application connectivity to an Autonomous Database, follow these guidelines to achieve high performance and continuous availability of your application's connections to the database:

For PHP, you enable FAN when you add the line oci8.events = On to the php.ini file as part of downloading and installing the Oracle OCI8 extension to PHP.

The OCI8 interface does not expose specific connection pooling calls. Instead, it uses connection pooling internally. To realize the benefits of connection pooling, use the oci_pconnect() function to create or reuse a persistent connection that remains across HTTP requests.

For example:

$connection = oci_pconnect("appuser", getenv("MY_PASSWORD_ENV_VAR"), "tp_tls");

This example creates a persistent connection to the tp_tls database service.

Additional Resources

For information about the Oracle OCI8 extension to PHP, see the Oracle OCI8 page.

For information about using PHP with Oracle Database, see The Underground PHP and Oracle Manual.