Constants

DBOF_DEBUGOFF

DBOF_DEBUGOFF

DEBUG: No Debug Info.

DBOF_DEBUGSCREEN

DBOF_DEBUGSCREEN

DEBUG: Debug to screen.

DBOF_DEBUGFILE

DBOF_DEBUGFILE

DEBUG: Debug to error.log.

DBOF_COLNAME

DBOF_COLNAME

DescTable(): Column name.

DBOF_COLTYPE

DBOF_COLTYPE

DescTable(): Column type.

DBOF_COLSIZE

DBOF_COLSIZE

DescTable(): Column size.

DBOF_COLPREC

DBOF_COLPREC

DescTable(): Column precision.

DBOF_CACHE_QUERY

DBOF_CACHE_QUERY

Query cache: Entry is a query.

DBOF_CACHE_STATEMENT

DBOF_CACHE_STATEMENT

Query cache: Entry is a statement handle.

DBOF_SHOW_NO_ERRORS

DBOF_SHOW_NO_ERRORS

Print_Error(): Supress error details.

DBOF_SHOW_ALL_ERRORS

DBOF_SHOW_ALL_ERRORS

Print_Error(): Display all error details.

DBOF_RETURN_ALL_ERRORS

DBOF_RETURN_ALL_ERRORS

Print_Error(): Return all errors, handle them on your own.

Properties

$sock

$sock : resource|null

Type

resource|null — Internal connection handle.

$host

$host : string

Type

string — The TNS name of the target database.

$user

$user : string

Type

string — The username used to connect to database.

$password

$password : string

Type

string — The password used to connect to database.

$appname

$appname : string

Type

string — The Name of the application using this class.

$debug

$debug : integer

Type

integer — Debugstate, default is self::DBOF_DEBUGOFF (off)

$sqlerr

$sqlerr : string

Type

string — Contains possible SQL query that failed.

$sqlerrmsg

$sqlerrmsg : string

Type

string — Contains oci_error['message'] info in case of an error.

$classversion

$classversion : string

Type

string — Class version

$querycounter

$querycounter : integer

Type

integer — How many queries where executed.

$querytime

$querytime : float

Type

float — Amount of time spent executing class methods.

$php_with_priv_connect

$php_with_priv_connect : boolean

Type

boolean — If php ini parameter "oci8.privileged_connect" is set or not.

$usePConnect

$usePConnect : boolean

Type

boolean — Set to TRUE if Connect() should use persistant connection, else new one (Default)

$connectRetries

$connectRetries : integer

Type

integer — How many retries the class should perform when connecting to Oracle. Defaults to 1 but can be overriden via the OCIDB_CONNECT_RETRIES define.

$SAPI_type

$SAPI_type : string

Type

string — The SAPI type of php (used to detect CLI sapi).

$errvars

$errvars : array

Type

array — All passed variables except QUERY and Flags.

$php532

$php532 : boolean

Type

boolean — TRUE if at least PHP 5.3.2 is running, required to set the various client/module/action things.

$AffectedRows

$AffectedRows : integer

Type

integer — How many Rows where affected by previous DML operation.

$output_hash

$output_hash : array

Type

array — A hash array with all output parameters (used in QueryHash()).

$sqlcache

$sqlcache : array

Type

array — Internal SQL cache for Prepare()/Execute().

$sqlcount

$sqlcount : integer

Type

integer — How many SQL queries have been executed.

$stmt

$stmt : resource|null

Type

resource|null — Stores active statement handle.

$showError

$showError : integer

Type

integer — Holds error mode.

$AdminEmail

$AdminEmail : string

Type

string — E-Mail address to be shown in error messages

Methods

__construct()

__construct(string  $ext_config = '') 

Constructor of class.

Parameters

string $ext_config

Pass here the full name to your define file where all external class defines are set. If empty uses "dbdefs.inc.php".

Connect()

Connect(string  $user = NULL, string  $pass = NULL, string  $host = NULL, integer  $exit_on_error = 1, string  $use_charset = '', integer  $session_mode = -1) : mixed

Performs the connection to Oracle.

If anything goes wrong calls Print_Error(). Also an Oracle procedure is called to register the Application name as defined in dbdefs.inc.php, This helps DBAs to better fine tune their databases according to application needs.

Parameters

string $user

Username used to connect to DB

string $pass

Password to use for given username

string $host

Hostname of database to connect to

integer $exit_on_error

If set to 1 Class will automatically exit with error code, else return error array

string $use_charset

Optional character set to use.

integer $session_mode

Optional the session mode (OCI_SYSOPER/OCI_SYSDBA).

Returns

mixed —

Either the DB connection handle or an error array/exit, depending how $exit_on_error is set

Disconnect()

Disconnect(mixed  $other_sock = -1) 

Disconnects from Oracle.

You may optionally pass an external link identifier.

Parameters

mixed $other_sock

Optionally your own connection handle to close, else internal socket will be used.

SetPConnect()

SetPConnect(boolean  $conntype) : boolean

Sets connection behavour.

If FALSE class uses oci_logon to connect. If TRUE class uses oci_plogon to connect (Persistant connection)

Parameters

boolean $conntype

TRUE => Enable persistant connections, FALSE => Disable persistant connections

Returns

boolean —

The previous state

GetPConnect()

GetPConnect() : boolean

Returns current persistant connection flag.

Returns

boolean —

The current setting (TRUE/FALSE).

GetConnectionHandle()

GetConnectionHandle() : resource

Returns current connection handle.

Returns either the internal connection socket or -1 if no active handle exists. Useful if you want to work with OCI* functions in parallel to this class.

Returns

resource —

Internal socket value

SetConnectionHandle()

SetConnectionHandle(resource  $extsock) 

Allows to set internal socket to external value.

Note that the internal socket descriptor is only overriden if the class has no active connection stored! If already a connection was performed the class does not override it's internal handle to avoid problems!

Parameters

resource $extsock

The connection handle as returned from oci_login().

SetModuleAction()

SetModuleAction(string  $module, string  $action = "", boolean  $returnURL = FALSE) : string

Registers the name either throught DBMS_APPLICATION_INFO package or by calling the new PHP 5.3.2+ functions.

Parameters

string $module

The module name to use, Connect() passes the Application name here (max. 48 characters).

string $action

Optional action info to use (max. 32 characters).

boolean $returnURL

If FALSE (default) and no PHP 5.3.2 is in use, calls "DBMS_APPLICATION_INFO()" directly, else returns the call to it.

Returns

string —

If PHP532 is used nothing is returned, else the ready-to-use PL/SQL call to DBMS_APPLICATION_INFO.SET_MODULE().

SetAction()

SetAction(string  $action) : boolean

Sets a given string as action (max. 32 bytes) to the current Oracle connection.

Note that this works only for PHP 5.3.2+ and OCI8 must be linked against Oracle 10g or newer, else returns TRUE without doing anything.

Parameters

string $action

Action info to use (max. 32 characters).

Returns

boolean —

TRUE on success, else FALSE.

SetClientInfo()

SetClientInfo(string  $cinfo) : boolean

Sets a given string as client identifier (max. 64 bytes) to the current Oracle connection.

Note that this works only for PHP 5.3.2+ and OCI8 must be linked against Oracle 10g or newer, else returns TRUE without doing anything.

Parameters

string $cinfo

Client info to use (max. 64 bytes).

Returns

boolean —

TRUE on success, else FALSE.

Query()

Query(string  $querystring, integer  $resflag = OCI_ASSOC, integer  $no_exit) : array|integer

Performs a single row query without Bindvar support.

Resflag can be OCI_NUM or OCI_ASSOC depending on what kind of array you want to be returned. WARNING!!!! All previous class versions up to 0.78 had support for Bind vars on this method! This is no longer supported and if you pass more than 3 parameters to this method a warning is generated and execution is halted. Please use ONLY (!) QueryHash() if you want to use bind variables.

Parameters

string $querystring

The query to be executed against the RDBMS

integer $resflag

OCI_NUM for numeric array or OCI_ASSOC (default) for associative array result

integer $no_exit

1 => Function returns errorcode instead of calling Print_Error() or 0 => Will always call Print_Error()

Returns

array|integer —

The result of the query as either associative or numeric array. In case of an error can be also an assoc. array of error informations.

QueryHash()

QueryHash(string  $querystring, integer  $resflag = OCI_ASSOC, integer  $no_exit,   $bindvarhash = null) : array|integer

Performs a single row query with Bindvar support passed as associative hash.

Resflag can be OCI_NUM or OCI_ASSOC depending on what kind of array you want to be returned. Remember to pass all required variables for all defined bind vars after the $no_exit parameter as an assoc. array (Key = name of bindvar without ':', value = value to add).

Parameters

string $querystring

The query to be executed against the RDBMS

integer $resflag

OCI_NUM for numeric array or OCI_ASSOC (default) for associative array result

integer $no_exit

1 => Function returns errorcode instead of calling Print_Error() or 0 => Will always call Print_Error()

$bindvarhash

Returns

array|integer —

The result of the query as either associative or numeric array. In case of an error can be also an assoc. array of error informations.

QueryResult()

QueryResult(string  $querystring) : mixed

Performs a multirow-query and returns result handle.

Required if you want to fetch many data rows. Does not return in case of error, so no further checking is required. NOTE: Bind Var support is deprecated and no longer supported, use QueryResultHash() instead!

Parameters

string $querystring

SQL-Statement to be executed

Returns

mixed —

Returns the statement handle or an error array in case of an error.

QueryResultHash()

QueryResultHash(string  $query,   $inhash) : mixed

Executes a query with parameters passed as hash values.

Also IN/OUT and RETURNING INTO <...> clauses are supported. You have to use FetchResult()/FreeResult() after using this function.

Parameters

string $query

The Query to be executed.

$inhash

Returns

mixed —

Either the statement handle or an error code / calling Print_Error().

FetchResult()

FetchResult(integer  $resflag = OCI_ASSOC, mixed  $extstmt = -1) : array

Fetches next datarow.

Returns either numeric (OCI_NUM) or associative (OCI_ASSOC) array for one data row as pointed to by either internal or passed result var.

Parameters

integer $resflag

OCI_ASSOC => Return associative array or OCI_NUM => Return numeric array.

mixed $extstmt

If != -1 then we try to fetch from that passed handle, else the class uses internal saved handle. Useful if you want to perform a lot of different queries.

Returns

array —

The fetched datarow or NULL if no more data exist.

FreeResult()

FreeResult(mixed  $extstmt = -1) : mixed

Frees result obtained by QueryResult().

You may optionally pass external Result handle, if you omit this parameter the internal handle is freed. This function also checks the built-in statement cache for the handle and removes it from cache, too.

Parameters

mixed $extstmt

Optional your external saved handle to be freed.

Returns

mixed —

The result of oci_free_statement() is returned.

AffectedRows()

AffectedRows() : integer

Returns count of affected rows.

Info is set in Query() and QueryResult() / FreeResult() and should return the amount of rows affected by previous DML command

Returns

integer —

Number of affected rows of previous DML command

Commit()

Commit(integer|resource  $extstmt = -1) : integer

Commits transaction.

Parameters

integer|resource $extstmt

Optional an external oracle connection resource handle, else the internal one will be used.

Returns

integer —

The value of oci_commit() is returned.

Rollback()

Rollback(integer|resource  $extstmt = -1) : integer

Rollback transaction.

Parameters

integer|resource $extstmt

Optional an external oracle connection resource handle, else the internal one will be used.

Returns

integer —

The value of oci_rollback() is returned.

SetDebug()

SetDebug(integer  $state) 

Function allows debugging of SQL Queries.

$state can have these values:

  • self::DBOF_DEBUGOFF = Turn off debugging
  • self::DBOF_DEBUGSCREEN = Turn on debugging on screen (every Query will be dumped on screen)
  • self::DBOF_DEBUFILE = Turn on debugging on PHP errorlog You can mix the debug levels by adding the according defines!

Parameters

integer $state

The DEBUG level to set

GetDebug()

GetDebug() : integer

Returns the current debug setting.

Returns

integer —

The current debug level.

PrintDebug()

PrintDebug(string  $msg) 

Handles debug output according to internal debug flag.

Parameters

string $msg

The string to be send out to selected output.

SQLDebug()

SQLDebug(boolean  $state) 

Allows to en- or disable the SQL_TRACE feature of Oracle.

Pass TRUE to enable or FALSE to disable. When enabled all Statements of your session are saved in a tracefile stored in $ORACLE_BASE/admin//udump/*.trc After your session disconnects use the tkprof tool to generate Human-readable output from the tracefile, i.e.: $> tkprof oracle_ora_7527.trc out.txt Now read 'out.txt' and see what happen in Oracle!

Parameters

boolean $state

TRUE to enable or FALSE to disable the SQL_TRACE feature.

GetSQLError()

GetSQLError() : array

Returns hash with error informations from last query.

Returns

array —

Assoc. array with error informations.

GetErrorText()

GetErrorText(  $exterr = "") : string

This method tries to get the description for a given error message.

Simply pass the $err['message'] field to this function, it tries to extract the required informations and call $ORACLE_HOME/bin/oerr to get the error description. If either the exterr or the internal sqlerrmsg variables are empty this function returns: "No error found."

Parameters

$exterr

Returns

string —

The extracted error text.

SetErrorHandling()

SetErrorHandling(integer  $val) : integer

Allows to set the handling of errors.

  • self::DBOF_SHOW_NO_ERRORS => Show no security-relevant informations
  • self::DBOF_SHOW_ALL_ERRORS => Show all errors (useful for development)
  • self::DBOF_RETURN_ALL_ERRORS => No error/autoexit, just return the OCI error code.

Parameters

integer $val

The Error Handling mode you wish to use.

Returns

integer —

Returns the old value.

GetErrorHandling()

GetErrorHandling() : integer

Returns the current error handling mode.

Returns

integer —

The current error handling mode.

GetConnectRetries()

GetConnectRetries() : integer

Returns the number of retries in case of connection problems.

This value can be set globally inside the dbdefs.inc.php file via the OCIDB_CONNECT_RETRIES define but may be changed run-time also via the "setConnectRetries()" method.

Returns

integer —

The retry counter value currently set.

SetConnectRetries()

SetConnectRetries(integer  $retcnt) : integer

Change the number of retries the class performs in case of connection problems.

This value is globally setable in the dbdefs.inc.php script (see define OCIDB_CONNECT_RETRIES) but can be set also run-time via this method.

Parameters

integer $retcnt

The new number of connect retries.

Returns

integer —

The previous value

GetQueryCount()

GetQueryCount() : integer

Returns amount of queries executed by this class.

Returns

integer —

How many queries are executed currently by this class.

GetQueryTime()

GetQueryTime() : float

Returns amount of time spend on queries executed by this class.

Returns

float —

Time in seconds.msecs spent in executing SQL statements.

getmicrotime()

getmicrotime() : float

Returns microtime in format s.mmmmm.

Used to measure SQL execution time.

Returns

float —

the current time in microseconds.

GetClassVersion()

GetClassVersion() : string

Returns version of this class.

Returns

string —

The version string in format "major.minor"

Version()

Version() : string

Returns Oracle Server Version.

Opens an own connection if no active one exists.

Returns

string —

The Oracle Release Version string

SetPrefetch()

SetPrefetch(integer  $rows, mixed  $extstmt = -1) : boolean

Allows to set the prefetch value when returning results.

Default is 1 which may lead to performance problems when data is transmitted via WAN.

Parameters

integer $rows

Amount of rows to be used for prefetching.

mixed $extstmt

Optionally your own statement handle. If you omit this parameter the internal statement handle is used.

Returns

boolean —

Return value of OCISetPrefetch()

Print_Error()

Print_Error(string  $ustr = '', mixed  $var2dump = NULL, integer  $exit_on_error = 1) : integer|null

Prints out an Oracle error.

Tries to highlight the buggy SQL part of the query and dumps out as much informations as possible. This may lead however to security problems, in this case you can set DBOF_SHOW_NO_ERRORS and the Error informations are returned to the callee instead of being displayed on-screen. An e-mail can be send if configured, for details see dbdefs.inc.php

Parameters

string $ustr

Optional user-error string to be displayed

mixed $var2dump

Optional a variable to be dumped out via print_r()

integer $exit_on_error

If set to default of 1 this function terminates execution of the script by calling exit, else it simply returns.

Returns

integer|null —

If self::DBOF_RETURN_ALL_ERRORS is set returns error code, else exit()s.

DescTable()

DescTable(string  $tablename) : array

Describes a table by returning an array with all table info.

Parameters

string $tablename

Name of table you want to describe.

Returns

array —

A 2-dimensional array with table informations.

SetOutputHash()

SetOutputHash(  $outputhash) 

Use this function to pass output hash data to QueryHash() function.

This is only required if you are using RETURNING INTO clauses or OUT variables, if you only use the bind variables for input (IN) you do not need to set this. WARNING: You are responsible to clear the array by using clearOutputHash()!

Parameters

$outputhash

GetOutputHash()

GetOutputHash() : array

Returns the contents of the output_hash variable.

Returns

array —

The contents of the internal output_hash variable.

ClearOutputHash()

ClearOutputHash() 

Clears the internal output hash array.

You are responsible to manage this yourself, the class only uses the variable!

Prepare()

Prepare(string  $querystring, integer  $no_exit) : mixed

Preparses a query but do not execute it (yet).

This allows to use a compiled query inside loops without having to parse it everytime. All prepared() queries will be put into our own QueryCache() so we can use the Prepare()/Execute()/ExecuteHash() pair for more than one query at once.

Parameters

string $querystring

The Query you want to prepare (can contain bind variables).

integer $no_exit

1 => Function returns errorcode instead of calling Print_Error() or 0 => Will always call Print_Error()

Returns

mixed —

Either the statement handle on success or an error code / calling print_error().

Execute()

Execute(mixed  $stmt, integer  $no_exit) : mixed

Executes a prepare()d statement and returns the result.

You may then Fetch rows with FetchResult() or call FreeResult() to free your allocated result. Execute() searches first our QueryCache before executing, this way we can use almost unlimited Queries at once in the Prepare/Execute pair.

Parameters

mixed $stmt

The statement handle to be executed.

integer $no_exit

1 => Function returns errorcode instead of calling Print_Error() or 0 => Will always call Print_Error()

Returns

mixed —

Returns result set read for FetchResult() usage or an error state depending on class setting in case of an error.

ExecuteHash()

ExecuteHash(mixed  $stmt,   $bindvarhash, integer  $no_exit) : mixed

Executes a prepare()d statement and returns the result.

You may then fetch rows with FetchResult() or call FreeResult() to free your allocated result. This method is almost identical to "Execute()" with the addition that bind variables are supported via an associative array.

Parameters

mixed $stmt

The statement handle to be executed.

$bindvarhash
integer $no_exit

1 => Function returns errorcode instead of calling Print_Error() or 0 => Will always call Print_Error()

Returns

mixed —

Returns result set read for FetchResult() usage or an error state depending on class setting in case of an error.

SaveBLOB()

SaveBLOB(string  $file_to_save, string  $blob_table, string  $blob_field, string  $where_clause, array  $bind_vars = null) : integer

Allows to save a file to a binary object field (BLOB).

Does not commit!

Parameters

string $file_to_save

Full path and filename of file to save

string $blob_table

Name of Table where the blobfield resides

string $blob_field

Name of BLOB field

string $where_clause

Criteria to get the right row (i.e. WHERE ROWID=ABCDEF12345)

array $bind_vars

If given can contain bind variable definition used in WHERE clause

Returns

integer —

If all is okay returns 0 else an oracle error code.

CheckSock()

CheckSock() 

Checks if we are already connected to our database.

If not terminates by calling Print_Error().

SearchQueryCache()

SearchQueryCache(mixed  $stmt) : integer

Searches internal query cache for given statement id.

Returns index of found statement id or -1 to indicate an error. This function is considered private and should NOT (!) be called from outside this class!

Parameters

mixed $stmt

The statement handle to search for

Returns

integer —

The index number of the found statement or -1 if no handle could be found.

RemoveFromQueryCache()

RemoveFromQueryCache(integer  $nr) 

Removes query from cache.

Tries to remove a query from cache that was found by a previous call to SearchQueryCache().

Parameters

integer $nr

Number of statement handle to be removed from cache.

SendMailOnError()

SendMailOnError(array  $errarray) 

Sends an error email.

If OCIDB_SENTMAILONERROR is defined and != 0 the class sent out an error report to the configured email address in case of an error.

Parameters

array $errarray

The error array from Oracle as returned by getSQLError()