简体   繁体   English

如何使用 php 连接到 Oracle 数据库?

[英]How to connect to Oracle Database with php?

I am currently working on a website that connects to an Oracle Database.我目前正在开发一个连接到 Oracle 数据库的网站。 I have two php files, one for connection with the database and the other is the html structure itself.我有两个 php 文件,一个用于连接数据库,另一个是 html 结构本身。

Connect.php:连接.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? 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()编辑 1:我使用了 display_errors,我得到的错误是 Call to undefined function oci_connect()

Edit 2: I tried everything at this point to make the oci_connect work.编辑 2:我此时尝试了一切以使 oci_connect 工作。 I downloaded the oracle client and made it an environmental variable but oci_connect is still not working.我下载了 oracle 客户端并将其设置为环境变量,但 oci_connect 仍然无法正常工作。 I would really appreciate if any mac users could help me with this.如果任何 mac 用户可以帮助我,我将不胜感激。

Download the appropriate oracle client to your machine, extract it and copy and paste in your system drive.将相应的 oracle 客户端下载到您的计算机,解压缩并复制并粘贴到您的系统驱动器中。

Add the path of your oracle client to environment variables.将 oracle 客户端的路径添加到环境变量中。

Then you need to enable php_oci8_12c extension using php.ini or GUI if available.然后,您需要使用 php.ini 或 GUI(如果可用)启用 php_oci8_12c 扩展。 Open php file and write the following code:打开 php 文件并编写以下代码:

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.此代码在我的 windows 10 机器、php 7.1.9 和 oracle 12c 上运行良好。

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/搜索您的确切问题后,谷歌中的第四个链接显示以下链接: 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];
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM