0

I researched and found out oci_connect() is the way to go. I found out that i could either use the Connect Name from the tnsnames.ora file or use an easy connect syntax. Since my database isn't locally stored and I had no idea where the said, tnsnames.ora file was located in apex.oracle.com, I went with easy connect strings.Here's what I've done so far.

    $username = "myemail";
    $host = "apex.oracle.com";
    $dbname = "name";
    $password = "password";

    // url = username@host/db_name

    $dburl = $username . "@".$host."/".$dbname;

    $conn = oci_connect ($username, $password, $dburl);

    if(!$conn) echo "Connection failed";

I get a

    Call to undefined function oci_connect()

So what would be the way to go?

UPDATE 1:

Here's the list of things I did:

  • Installed Oracle DB

  • Unzipped Oracle Instance client

  • Set the environment variables

  • Uncommented the extension=php_oci8_12c.dll in php.ini

  • Copied all the *.dll files from the instance client folder to xampp/php and xampp/apache/bin

  • also made sure the php/ext folder had the required dlls.

That was last night. I have restarted my PC multiple times, APACHE with it but I'm still getting this error:

    Call to undefined function oci_connect()

At this point I'm frustrated and don't know where to go from here. PHP just doesn't seem to link up to oci8. I can view the databases I made from Database Configuration Assistant in cmd from 'sqlplus' command and a few select statements. So everything seems to be setup right, its just the php that's having problems trying to use oci_connect(). My database.php, now is setup as:

public function __construct()
{
    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     = 'haatbazaar';
    $db_username = "SYSTEM";
    $db_password = "root";

    $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);
    }
}

And it outputs:

oci_connect not found

OCI8 is listed in my phpInfo().

TheFlyBiker 420
  • 69
  • 1
  • 13

2 Answers2

1

Okay I found out the culprit behind this whole ordeal. I had set the PATH Environment Variables but apparently forgot to add a new system environment variable named TNS_ADMIN and set the directory to PATH/TO/INSTANCE/CLIENT. Here's the list of System Environment variable you need to add:

  • Edit PATH system variable and add the $ORACLE_HOME/bin dir
  • Edit PATH system variable and add the Instance Client dir
  • Add new system variable, name it TNS_ADMIN and add the Instance Client dir

I hope this helps those who come looking.

TheFlyBiker 420
  • 69
  • 1
  • 13
  • 1
    It adds confusion since your original post uses Easy Connect syntax or a full connect string hardcoded in the app: neither of these need TNS_ADMIN set. Also with Instant Client you don't need to (i.e. shouldn't) set ORACLE_HOME. Finally, if your database is on your local machine, you don't Instant Client at all. This is all discussed in various install manuals and also sections of http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html – Christopher Jones Mar 18 '18 at 22:28
  • I know instance client is not needed if the database is locally stored but I had tried everything and not to mention, it did start working after adding the 'TNS_ADMIN' environment variable, so there's that. – TheFlyBiker 420 Mar 20 '18 at 10:44
0

First, this has been asked before, but Oracle doesn't allow remote database connections to their free apex.oracle.com example service. Sorry. You can only interact with it through the web interface.

Second, if you do find a remote Oracle db to connect to, you'll need to install the Oracle Instant Client for your OS, and configure the PHP OCI8 extension.

kfinity
  • 8,581
  • 1
  • 13
  • 20
  • I actually did everything, installed oracle db, unzipped instance client, set the environment variables and uncommented the extension=php_oci8_12c.dll in php.ini and copied all the .dll files from the instance client folder to xampp/php and xampp/apache/bin and also made sure the php/ext folder had the required dlls. That was last night. I have restarted my PC multiple times, APACHE with it but I'm still getting this error: Call to undefined function oci_connect() – TheFlyBiker 420 Mar 17 '18 at 10:55