This source code shows you how to connect to Oracle Database 11g and execute queries using the PHP. First of all it sets the ORACLE_HOME and ORACLE_SID Environment variables using the putenv() method.

Note that to run this code, you will need PHP OCI8 extension enable in php.ini file.

//connect to server
if(!($Connection = oci_connect("user", "password")))
	print("Could not connect to database!\n");

$Query = "SELECT * FROM emp ";

//parse query
$stid = oci_parse($Connection , $Query);

// execute query
$r = oci_execute($stid);

// Fetch each row in the array
print '------------------table-----------------';
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
   print '\n';
   foreach ($row as $item) {
       print ' - '.($item !== null ? htmlentities($item, ENT_QUOTES) : ' ').' ';

print '------------------/table-----------------';

// Close the Oracle connection

Here are some of the common OCI8 functions which are used to perform different operations using the PHP Source Code.

oci_bind_array_by_nameBinds PHP array to Oracle PL/SQL array by name
oci_bind_by_nameBinds the PHP variable to the Oracle placeholder
oci_cancelCancels reading from cursor
oci_closeCloses Oracle connection
oci_commitCommits outstanding statements
oci_connectEstablishes a connection to the Oracle server
oci_define_by_nameUses a PHP variable for the define-step during a SELECT
oci_errorReturns the last error found
oci_executeExecutes a statement
oci_fetch_allFetches all rows of result data into an array
oci_fetch_arrayReturns the next row from the result data as an associative or numeric array, or both
oci_fetch_assocReturns the next row from the result data as an associative array
oci_fetch_objectReturns the next row from the result data as an object
oci_fetch_rowReturns the next row from the result data as a numeric array
oci_fetchFetches the next row into result-buffer
oci_field_is_nullChecks if the field is NULL
oci_field_nameReturns the name of a field from the statement
oci_field_precisionTell the precision of a field
oci_field_scaleTell the scale of the field
oci_field_sizeReturns the size of the field
oci_field_type_rawTell the raw Oracle data type of the field
oci_field_typeReturns data type of the field
oci_free_statementFrees all resources associated with statement or cursor
oci_internal_debugEnables or disables internal debug output
oci_new_collectionAllocates new collection object
oci_new_connectEstablishes a new connection to the Oracle server
oci_new_cursorAllocates and returns a new cursor (statement handle)
oci_new_descriptorInitializes a new empty LOB or FILE descriptor
oci_num_fieldsReturns the number of result columns in a statement
oci_num_rowsReturns number of rows affected during statement execution
oci_parsePrepares Oracle statement for execution
oci_password_changeChanges password of Oracle’s user
oci_pconnectConnect to an Oracle database using a persistent connection
oci_resultReturns a field’s value from a fetched row
oci_rollbackRolls back outstanding transaction
oci_server_versionReturns server version
oci_set_prefetchSets number of rows to be prefetched
oci_statement_typeReturns the type of an OCI statement