0

I am currently working on a website that connects to an Oracle Database. I have two php files, one for connection with the database and the other is the html structure itself.

Connect.php:

<?php

$servername = "//////";
$username = "/////";
$password = "/////";

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

 if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
 }
 echo "Connected!";

?>

I have been having a very hard time connecting to the database. I followed the Oracle tutorial and edited the oci8.connection_class = MYPHPAPP in php.ini, but everytime I run the Connect.php, I get the HTTP Error 500. Did I miss anything? What should I do?

Edit 1: I used display_errors and the error I am getting is Call to undefined function oci_connect()

Edit 2: I tried everything at this point to make the oci_connect work. I downloaded the oracle client and made it an environmental variable but oci_connect is still not working. I would really appreciate if any mac users could help me with this.

  • Check you have error reporting enabled - https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display. This may give some more information. – Nigel Ren Jun 02 '20 at 07:50
  • Your operating system, php version, and oracle version please? – Hardood Jun 02 '20 at 07:57
  • @NigelRen I found display_errors in php.ini but it seems to be set on => display_errors = MAMP_display_errors_MAMP –  Jun 02 '20 at 08:38
  • @Hardood I am using macOS Mojave, Oracle 12c and php 7.4.2. –  Jun 02 '20 at 08:44
  • @NigelRen thanks for the comment. The error I am getting is undefined oci_connect() –  Jun 02 '20 at 08:46
  • Have a look at https://stackoverflow.com/questions/22478387/call-to-undefined-function-oci-connect to see if that helps. – Nigel Ren Jun 02 '20 at 08:48

2 Answers2

1

Forth link in google after searching for your exact questions brought up the following link: http://me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/

<?php
    $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;

    if($c = OCILogon("system", "your database password", $db))
    {
        echo "Successfully connected to Oracle.\n";
        OCILogoff($c);
    }
    else
    {
        $err = OCIError();
        echo "Connection failed." . $err[text];
    }
?>
Makwana Prahlad
  • 1,025
  • 5
  • 20
  • Hey, this code has the same problem I asked in the question above. I keep getting an error "Call to undefined function ocilogon()". I guess at this point I just need a help with adding and oci library to my php.ini –  Jun 03 '20 at 05:34
0

Download the appropriate oracle client to your machine, extract it and copy and paste in your system drive.

Add the path of your oracle client to environment variables.

Then you need to enable php_oci8_12c extension using php.ini or GUI if available. Open php file and write the following code:

function connect(){
     $dburl = "(DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = your_host)(PORT = 1521))
     (CONNECT_DATA =
       (SERVER = DEDICATED)
       (SERVICE_NAME = your_db_sid)
     )
     )";
     //db charset is optional
     $db_charset = 'WE8MSWIN1252'; //your db charset goes here
     try {
         return oci_connect("username", "password",$dburl,$db_charset);
       } 
       catch (Exception $e) {
         die($e);
       }
   }

This code is work fine on my windows 10 machine, php 7.1.9 and oracle 12c.

Hardood
  • 503
  • 1
  • 5
  • 15
  • I made the path to the client an environmental variable but I cannot seem to find php_oci8_12c extension line in php.ini . I saw ;oci8.privileged_connect = Off, should uncomment and change the value to on? –  Jun 02 '20 at 09:51
  • I have no idea about how to configure PHP on macOS, so try enable anything related to oci in your php.ini file. – Hardood Jun 02 '20 at 10:01