PHPackages                             spfalz/db\_oci8 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. spfalz/db\_oci8

ActiveLibrary[Database &amp; ORM](/categories/database)

spfalz/db\_oci8
===============

Oracle database class using the OCI8 extension

1.1.0(7y ago)013BSD-2-Clause-FreeBSDPHPPHP &gt;=5.3.2||^7.0

Since Aug 12Pushed 7y ago1 watchersCompare

[ Source](https://github.com/SieGeL2k16/db_oci8)[ Packagist](https://packagist.org/packages/spfalz/db_oci8)[ Docs](https://github.com/SieGeL2k16/db_oci8/)[ RSS](/packages/spfalz-db-oci8/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

PHP OCI8 Class written by Sascha 'SieGeL' Pfalz
-----------------------------------------------

[](#php-oci8-class-written-by-sascha-siegel-pfalz)

Last Updated: 12-Aug-2018

### INTRODUCTION

[](#introduction)

This class was mainly created to reduce the code size of complex PHP applications that interact with Oracle Databases. Instead of writing down always the same procedures like OCILogin()/OCIParse() etc. the class provides a much simpler way of using Oracle while still supporting bind vars, BLOBs and various other helpful methods.

This doc lists the various configuration methods and gives a small overview how the class works, what to do in case of an error etc. For examples how to use this class please refer to the examples directory, I have supplied various examples that show how to use the class. In addition you can download the "Oracle Information Site" package from my homepage, this little application shows how to use this class in a real world example.

Note that since the 1.00 release two versions of the class are shipped within the distribution archive to allow either the usage of the class under PHP 4 or PHP 5. New projects however should always use the PHP 5 class, as the PHP 4 is at end-of-life. See also the supplied file "PHP4\_PHP5\_changes.txt" for details about the differences between the two versions of this class.

#### FILES PROVIDED IN THIS RELEASE

[](#files-provided-in-this-release)

The distribution archive contains several directories and files, here's an overview of these files:

- **CHANGELOG.md** -&gt; Changelog in chronological order
- **\_create\_docs.sh** -&gt; Small bash script to create PHPDocumentor docs
- **dbdefs.inc.php** -&gt; Configuration file for the PHP 5 class
- **db\_oci8.class.php** -&gt; The PHP 5 class
- **docs/** -&gt; PHPDocumentor documentation in HTML format
- **examples/** -&gt; Contains various examples for this class
- **LICENCE** -&gt; The BSD licence
- **PHP4/** -&gt; Contains PHP 4 class + config file
- **PHP4\_PHP5\_changes.txt** -&gt; Readme explains differences between the PHP 4 and the PHP 5 version of the db\_oci8 class.
- **README.md** -&gt; The file you are currently reading

### REQUIREMENTS

[](#requirements)

This class require the following components:

- PHP 4/5/7 (last tested on PHP 7.1.1) - Note that the distribution archive now ships PHP 4 AND PHP 5 versions of the class!
- Oracle 8i, 9i, 10g or 11g (last tested on 11gR2)
- Optionally the instantclient package (tested with 10g/11g/12g Instantclient)

### INSTALLATION AND USAGE

[](#installation-and-usage)

#### Using Composer

[](#using-composer)

Just call the following command to add this class to your project:

`composer require spfalz/db_oci8 `

#### Manual installation

[](#manual-installation)

Copy the supplied class file to a directory of your choice. If you still use PHP 4, use the file "PHP4/oci8\_class.php", for PHP 5 and newer use the file "db\_oci8.class.php".

Also copy the file dbdefs.inc.php to the same directory you have copied the class file. The dbdefs\_inc.php file serves as the configuration file for the class. You may give an alternate path to this file in the constructor of this class.

The following defines can/must be set to use the class inside dbdefs.inc.php:

**OCIDB\_USER \[required\]**

- Oracle Username used as default connection, mandatory.

**OCIDB\_PASS \[required\]**

- Oracle Password for useraccount above, mandatory.

**OCIDB\_HOST \[optional\]**

- TNS Name of target database. For correct names you may simply try to connect locally to your Oracle DB with SQL*Plus, the name you have used on the SQL*Plus connection string should also work here. If your environment is correctly configured you can leave out this parameter, the ORACLE\_SID is then used to connect local to your oracle DB if your PHP runs on the same host as the Database. You can also set TWO\_TASK to your TNS name in case you use Instantclient.

**OCIAPPNAME \[required\]**

- Name of your application, mandatory. This is used in error messages and also to register this name to Oracle if you set `DB_REGISTER = 1`

**DB\_REGISTER \[optional\]**

- When set to 1 the class automatically calls the PL/SQL package `DBMS_APPLICATION_INFO.SET_MODULE()` to register the name of your application to Oracle (OCIAPPNAME define). This is useful to track Queries and stats inside Oracle. Set to 0 to disable auto register. If you are running PHP 5.3.2 or newer the class won't use anymore the `DBMS_APPLICATION_INFO` methods,instead the new functions `oci_set_module_name()` and `oci_set_action()` are used. See description of the class method `SetModuleAction()` for details.

**DB\_NUM\_DECIMAL / DB\_NUM\_GROUPING \[optional\]**

- Both flags are required to handle numeric values in localized environments, i.e. germans are using the '.' as grouping character and the ',' as decimal point, while americans do exactly the opposite. To avoid problems when your application makes use of `setlocale()` and other locale specific things you must enter the appropiate characters for your language to use. The class will then issue an `ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ` after successfully connecting to the database.

**DB\_ERRORMODE \[optional\]**

- This flag is used to configure the handling of errors. Whenever an error occure the class automatically shows a box with a lot of informations, i.e. the query used, the position of the first error inside the query, the query counter, filename of PHP script etc, and the error is also reported to the PHP error log with the **OCIAPPNAME** in front of the output. It also may send an email to a given account to inform the developer automatically. Finally the last transaction is rolled back and the script execution is terminated. However it maybe not always feasible to have this automatic error handling active, maybe you want to handle everything on your own or you want to hide internal facts to the customer in productive environments.

    You have currently three choices to set here:

    **\\spfalz\\db\_oci8::DBOF\_SHOW\_NO\_ERRORS (0)**

    This hides important informations like the query which produced this error. Only the OCI error code, the PHP scriptname and the error description is shown, everything else is hidden. Useful for productive enviroments. This is also the default setting if not configured.

    **\\spfalz\\db\_oci8::DBOF\_SHOW\_ALL\_ERRORS (1)**

    This lists a lot more informations like query etc. Very useful when developing a PHP application, as this should easily show where the error occured. But be warned, the user may see informations he shouldn't normally see (query i.e.), so make sure that you disable this for productive environments!

    **\\spfalz\\db\_oci8::DBOF\_RETURN\_ALL\_ERRORS (2)**

    In this mode all errors are returned with their OCI error code, the class does not exit nor abort the program execution, you are on your own to handle the returned error. This is useful to use the class in automatic scripts like some kind of CSV im-/export script.

**DB\_DEFAULT\_PREFETCH \[optional\]**

- Allows to set how many rows should be prefetched when querying data. PHP's default value is 1 row, you can set your own value here to override the default PHP setting.

**OCIDB\_ADMINEMAIL \[optional\]**

- Set here a valid email address where you want to have error reports sent to. To let this work you have to set **OCIDB\_SENTMAILONERROR = 1**. Whenever the class encounters an error and automatic error handling is activated, the class sends out an email to this address containing the error description. If not given and **OCIDB\_SENTMAILONERROR = 1** the `$_SERVER['SERVER_ADMIN']` variable contents is used as email address.

**OCIDB\_SENTMAILONERROR \[optional\]**

- Flag indicating if the class should auto-send emails to the defined EMail address whenever an error occures. Set it to 1 to enable auto-sending, and set it to 0 to disable this behavour.

**OCIDB\_MAIL\_EXTRAARGS \[optional\]**

- Use this define to pass additional parameter to the `mail()` command in `SendmailOnError()`. Some servers might need to set the -f parameter when using PHP's mail() command, and to allow this also here in the class you can use this define. Default is unset.

**OCIDB\_USE\_PCONNECT \[optional\]**

- if set to 1 persistant connections are used, else standard connects are used. This can be set also on a script-by-script basis via the method `setPConnect()`.

**OCIDB\_CONNECT\_RETRIES \[optional\]**

- Defines how many connection retries the class should perform before giving up with an appropiate error message. This is useful if your connection to the database is of bad quality. The class auto-retries the connection up to the number you define here. Default value is 1, that means that the class will only try one connection attempt, if this fails an error is generated. This is also the behavour of all previous class versions.

**OCIDB\_CHARSET \[optional\]**

- Setup a default characterset to be used during the connection. Note that this works Only for Oracle &gt;= 9.2 and PHP 5.1.2+. If this define is not set the NLS\_LANG environment variable value is used. You can also override this when using the `connect()` method.

The file dbdefs.inc.php is automatically included by the class once you instantiate it the first time. See supplied file for a live example how to use these defines.

To use the class you have to `require()` first the class code, the rest is done automatically when you first instantiate the class. Normally you may have one PHP script which includes several others, here would be the ideal place to put the require() statement for the class, i.e.:

```
// ...Your require() statements

require_once("path/to/db_oci8.class.php");

// ..Rest of your code here

```

Once this is done and you have added the proper values in dbdefs.inc.php you can now start using the class, this would look like this for example:

```
