3

I'm trying to connect oracle database to my laravel application using this package: https://github.com/yajra/laravel-oci8 but having error with the external connection, this is the error: enter image description here

Here the line of error in package: https://github.com/yajra/laravel-oci8/blob/5.8/src/Oci8/Connectors/OracleConnector.php#L35

System details

  • Operating System: macOS Mojave Version 10.14.4 (18E226)
  • PHP Version: 7.3
  • Laravel Version: 5.8.*
  • Laravel-OCI8 Version: 5.8.*

Anyone can help me please?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Muh Ghazali Akbar
  • 1,169
  • 3
  • 13
  • 21

3 Answers3

2

Looks like the predefined constants for your OCI8 extension is missing. Make sure that the OCI8 extension was either

  1. Correctly compiled into PHP (See https://www.orware.com/blog/tips-and-how-tos/oracle/oracle-oci8-php-installation)

  2. Dynamically loaded at runtime. You can check this in your script and an example of how to do this is shown here: https://www.sitepoint.com/php-extension-not-loaded/

Ling Toh
  • 2,404
  • 1
  • 16
  • 24
  • I have installed the oracle extension and related stuffs. – Muh Ghazali Akbar May 14 '19 at 20:17
  • Verify that the OCI8 extension is loaded using phpinfo(). If it is loaded and this is still not working, try loading the module at runtime using the dl() function, e.g., https://www.php.net/manual/en/function.dl.php – Ling Toh May 14 '19 at 21:45
  • 2
    To add to this Answer, the error in question can result from required Instant Client environment variables (e.g., `ORACLE_HOME` and/or `LD_LIBRARY_PATH`) not being defined in the effective environment, or the `oci8` extension not being enabled in the effective INI configuration (i.e., you are enabling the extension in the wrong `.ini` file). As was suggested, using `phpinfo()` is the best way to confirm both that the required environment variables are set and which `.ini` is effective given the operating environment (PHP-FPM vs. PHP-CLI vs. `phpdbg`, etc.). – Ben Johnson Dec 17 '19 at 16:07
0

There are frustratingly few tutorials on how to do this installation, which has lead to many a headache when installing this on new servers.

So, here's a quick rundown on how to get this installed on a ubuntu 20.04 server.

EDIT: I don't own a mac...
...but the commands should be very similar. At the very least, a quick google will turn up some tutorials on installing the prerequisites.
You may also need to figure out the different php installation locations (/etc/php is for linux)
Here's a tutorial on php extension installations for some of those details: https://affinitybridge.com/blog/adding-php-extensions-system-php-under-os-x-1015-catalina

note that you might be using different instantclient and PHP versions

prerequisites:

apt-get install unzip php-pear php7.4-dev
  1. Download the instantclient from oracle. https://www.oracle.com/database/technologies/instant-client/downloads.html. You're looking for the 'basic' and the 'sdk' downloads for your system/version
  2. unzip those into a directory. I'm using /opt/oracle.
    unzip /instantclient-basic.zip -d /opt/oracle
    unzip /instantclient-sdk.zip -d /opt/oracle 
    
  3. symlink libraries
    ln -s /opt/oracle/instantclient_11_2/libclntsh.so.11.1 /opt/oracle/instantclient_11_2/libclntsh.so
    ln -s /opt/oracle/instantclient_11_2/libocci.so.11.1 /opt/oracle/instantclient_11_2/libocci.so 
    
  4. make sure the oci.ini file exists in the mods-available directory
    echo 'extension=oci8.so' > /etc/php/7.4/mods-available/oci.ini
    
  5. symlink the extension files.
    ln -s /etc/php/7.4/mods-available/oci.ini /etc/php/7.4/cli/conf.d/10-oci8.ini
    ln -s /etc/php/7.4/mods-available/oci.ini /etc/php/7.4/fpm/conf.d/10-oci8.ini 
    
  6. define the required environment variables
    ORACLE_HOME=/opt/oracle/instantclient_11_2/
    LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2/
    
  7. install the extension
    echo "instantclient,$ORACLE_HOME" | pecl install oci8-2.2.0
    
  8. make sure ld knows about oracle
    echo $ORACLE_HOME > /etc/ld.so.conf.d/oracle-instantclient.conf
    ldconfig
    
  9. clean up
    rm -rf /instantclient-*.zip \
    

The problem I consistently got hung up on was related to only setting up the extension for php cli OR php fpm and not both. If you don't set it up for both, you'll see the OCI constant error somewhere.

SO, don't skip step 5. ;)

PS If you find yourself getting hung up on something, lets work together and update this answer so we can all be happier about installing this extension.

Beefjeff
  • 371
  • 4
  • 12
-2

In your php.ini file find and uncomment the following line.

extension=oci8_12c

Then, restart your web server. And/Or PHP container - if you are using one, e.g. php-fpm, hhvm.

Then, check if the extension is loaded by running <?php phpinfo(); ?> or at CLI > php -m

If the extension is not loaded, set Oracle client home directory in PATH environment variable, then try again.

If you are on Windows, try Windows restart if none of the above worked.

AnkitK
  • 388
  • 3
  • 10