Skip to content

Instantly share code, notes, and snippets.

@PhouvanhKCSV
Forked from odan/php-oracle.md
Created September 6, 2017 08:48
Show Gist options
  • Save PhouvanhKCSV/291b7234c2c582c8007016b73327da78 to your computer and use it in GitHub Desktop.
Save PhouvanhKCSV/291b7234c2c582c8007016b73327da78 to your computer and use it in GitHub Desktop.
XAMPP - Oracle Driver Setup (v12)

XAMPP - Oracle Driver Setup (v12)

Setup

Testest with PHP 5.6, 7.0, 7.1.

  • Download the Oracle Instant Client for Microsoft Windows (32-bit) v.12 http://www.oracle.com/technetwork/topics/winsoft-085727.html
  • Unzip file: instantclient-basiclite-nt-12.2.0.1.0.zip
  • Copy all *.dll files: to c:\xampp\php
  • Copy all *.dll files to c:\xampp\apache\bin (Yes, a second copy!)
  • Enable php extension in php.ini: extension=php_oci8_12c.dll
  • Restart Apache
  • Click at the Github ★ Star :-)

Connection test

<?php

error_reporting(E_ALL);

if (function_exists("oci_connect")) {
    echo "oci_connect found\n";
} else {
    echo "oci_connect not found\n";
    exit;
}

$host = 'localhost';
$port = '1521';

// Oracle service name (instance)
$db_name     = 'DBNAME';
$db_username = "USERNAME";
$db_password = "123456";

$tns = "(DESCRIPTION =
	(CONNECT_TIMEOUT=3)(RETRY_COUNT=0)
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = $host)(PORT = $port))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = $db_name)
    )
  )";
$tns = "$host:$port/$db_name";

try {
    $conn = oci_connect($db_username, $db_password, $tns);
    if (!$conn) {
        $e = oci_error();
        throw new Exception($e['message']);
    }
    echo "Connection OK\n";
    
    $stid = oci_parse($conn, 'SELECT * FROM ALL_TABLES');
    
    if (!$stid) {
        $e = oci_error($conn);
        throw new Exception($e['message']);
    }
    // Perform the logic of the query
    $r = oci_execute($stid);
    if (!$r) {
        $e = oci_error($stid);
        throw new Exception($e['message']);
    }
    
    // Fetch the results of the query
    while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
        $row = array_change_key_case($row, CASE_LOWER);
        print_r($row);
        break;
    }
    
    // Close statement
    oci_free_statement($stid);
    
    // Disconnect
    oci_close($conn);
    
}
catch (Exception $e) {
    print_r($e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment