php 将 PDO 与 Oracle 数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25712045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:58:01  来源:igfitidea点击:

Connect PDO with Oracle database

phporaclepdo

提问by Sizzling Code

I am new to Oracle, installed the Oracle today the 11g Express Edition. Then I installed Java SDK, and then the free Oracle SQL Developer. I connected with system account and created a username and table as defined below. I don't exactly know how Oracle works, I think instead of database name, usernames are used. So below are details.

我是 Oracle 新手,今天安装了 Oracle 11g Express Edition。然后我安装了 Java SDK,然后是免费的 Oracle SQL Developer。我与系统帐户连接并创建了如下定义的用户名和表。我不完全知道 Oracle 是如何工作的,我认为使用用户名而不是数据库名称。所以下面是细节。

Username/Connection/Database = CustomSearch
Table = Reservation_General_2

用户名/连接/数据库 =CustomSearch
表 =Reservation_General_2

There are some columns inside that table and some data. but the point is I cant connect to Oracle Server.

该表中有一些列和一些数据。但关键是我无法连接到 Oracle Server。

Here is how I tried to connect to database server.

这是我尝试连接到数据库服务器的方式。

<?php
/**
 * Created by PhpStorm.
 * User: HaiderHassan
 * Date: 9/3/14
 * Time: 9:52 PM
 */
header('Access-Control-Allow-Origin: *');
$tns = "
(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )
       ";
try {
    $conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

Problem is when I try to open that page, I get this error.

问题是当我尝试打开该页面时,出现此错误。

ERROR: could not find driver

错误:找不到驱动程序

These are my connection settings when I connect from Oracle Sql Developer.

这些是我从Oracle Sql Developer.

enter image description here

enter image description here

What am I doing wrong, what steps should I take to fix this issue?

我做错了什么,我应该采取什么步骤来解决这个问题?

Update

更新

I added the driver by removing semicolon from the php.inifile

我通过从php.ini文件中删除分号来添加驱动程序

extension=php_pdo_oci.dll 

But I started getting this error.

但我开始收到这个错误。

The program can't start because OCI.dll is missing from your computer. Try reinstalling the program to fix this problem.

程序无法启动,因为您的计算机缺少 OCI.dll。尝试重新安装程序以解决此问题。

I have to click 4 time OK for different alert boxes that shows up. I also downloaded oci.dlland copied it to the windows/system32, but still getting this error. What to do?

对于出现的不同警报框,我必须单击 4 次确定。我也下载oci.dll并将其复制到windows/system32,但仍然出现此错误。该怎么办?

Update

更新

I uninstalled XAMPP and followed this guide to install Apache and PHP separately,

我卸载了 XAMPP 并按照本指南分别安装了 Apache 和 PHP,

http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-12c-2088811.html

and then I tried my luck. That driver Problem went away but there is new problem

然后我试试运气。那个驱动程序问题消失了,但出现了新问题

ERROR: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12521: TNS:listener does not currently know of instance requested in connect descriptor (ext\pdo_oci\oci_driver.c:635)

错误: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12521: TNS:listener 当前不知道连接描述符 (ext\pdo_oci\oci_driver.c:635) 中请求的实例

Here below is my new connection String.

下面是我的新连接字符串。

try {
    $conn = new PDO('oci:dbname=//localhost:1521/xe/ORCL', 'customsearch', 'babaji');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

I tried to follow this answer on Stack Overflow for making a connection string.

我试图按照 Stack Overflow 上的这个答案来制作连接字符串。

http://stackoverflow.com/questions/11970261/connect-oracle-with-pdo-with-sid-and-instance-name

Update 2

更新 2

Also tried to check if drivers installed. I used this code

还尝试检查是否安装了驱动程序。我用了这个代码

foreach(PDO::getAvailableDrivers() as $driver)
    echo $driver, '\n';

Got this code from this below link

从下面的链接中得到了这个代码

http://stackoverflow.com/questions/23239433/could-not-connect-to-oracle-using-pdo

it echoes this below line

它与下面这行相呼应

oci\n

So this means that it is installed or this means some drivers are missing?

所以这意味着它已安装还是意味着缺少某些驱动程序?

Update 3

更新 3

Again rolled back to old connection just changed some stuff in that connection and seems like connection to oracle worked.

再次回滚到旧连接只是更改了该连接中的一些内容,似乎与 oracle 的连接有效。

try {
    $conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo 'Connected to database';
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

with this I get the message 'Connected to database', means echo works because there is no error given by PDO.

有了这个,我收到消息“已连接到数据库”,这意味着 echo 有效,因为 PDO 没有给出错误。

But problem is now my query is not working? What happened to my query? Or will I have to change the syntax of the query also as I connected to Oracle? Or is the connection still not working?

但问题是现在我的查询不起作用?我的查询怎么了?或者我是否也必须在连接到 Oracle 时更改查询的语法?或者连接仍然无法正常工作?

回答by Shailesh Sonare

Check PDO and OCI drivers installed properly or not

检查 PDO 和 OCI 驱动程序安装是否正确

Try with following code

尝试使用以下代码

class PDOConnection {

    private $dbh;

    function __construct() {
        try {

            $server         = "127.0.0.1";
            $db_username    = "SYSTEM";
            $db_password    = "Oracle_1";
            $service_name   = "ORCL";
            $sid            = "ORCL";
            $port           = 1521;
            $dbtns          = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";

            //$this->dbh = new PDO("mysql:host=".$server.";dbname=".dbname, $db_username, $db_password);

            $this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function select($sql) {
        $sql_stmt = $this->dbh->prepare($sql);
        $sql_stmt->execute();
        $result = $sql_stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    public function insert($sql) {
        $sql_stmt = $this->dbh->prepare($sql);
        try {
            $result = $sql_stmt->execute();
        } catch (PDOException $e) {
            trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
        }
        if ($result) {
            return $sql_stmt->rowCount();
        }
    }

    function __destruct() {
        $this->dbh = NULL;
    }

}

$dbh = new PDOConnection();

$dbh->select($select_sql);
$dbh->insert($insert_sql);

回答by Leo Bedrosian

Have you installed the PDO driver? Look at the output of phpinfo()to see what's installed and/or enabled in your environment.

您是否安装了 PDO 驱动程序?查看输出phpinfo()以查看您的环境中安装和/或启用的内容。

PDO Installation

PDO 安装

If you're running PHP on linux, you can see what PDO drivers are available for your distribution by running yum list php-pdo. You can install the driver by running yum install php-pdo. You may also need to install a database specific driver for your database. Running a yum list php*will show you all the PHP extensions available for installation.

如果您在 Linux 上运行 PHP,则可以通过运行yum list php-pdo. 您可以通过运行安装驱动程序yum install php-pdo。您可能还需要为您的数据库安装特定于数据库的驱动程序。运行 ayum list php*将显示所有可用于安装的 PHP 扩展。

Database Specific Drivers

数据库特定驱动程序

回答by Sirious Mouth

You need to install instant client on Windows, I used it and it work, see this video, the only that change is in the video when he execute install, you don't have to because in the new zip, doesn't have the execution file. I only have a problem when I make a SELECT query but the connection works just fine.

你需要在 Windows 上安装即时客户端,我用过它并且它工作,看这个视频,唯一的变化是在他执行安装时的视频中,你不必因为在新的 zip 中,没有执行文件。我只在进行 SELECT 查询时遇到问题,但连接工作正常。

https://www.youtube.com/watch?v=cZDDI9HFBIU

https://www.youtube.com/watch?v=cZDDI9HFBIU

Contact me if you have any question

如果您有任何问题,请联系我

回答by user628176

I think your problem is with oracle listener configuration, your driver is ok, the error "listener does not currently know of inst.." means there is oracle configuration issue. You must ensure that the parameters in the listener file is exactly the same as in the connection string.

我认为您的问题与 oracle 侦听器配置有关,您的驱动程序没问题,错误“侦听器当前不知道 inst ..”意味着存在 oracle 配置问题。您必须确保侦听器文件中的参数与连接字符串中的参数完全相同。

Also your connection string oci:dbname=//localhost:1521/xe/ORCL is incorrect, it should be oci:dbname=//localhost:1521/orcl (host:port/service_name) as indicated in listener.ora file. Ensure the correctness of your connection string using SQL developer.

此外,您的连接字符串 oci:dbname=//localhost:1521/xe/ORCL 不正确,它应该是 listener.ora 文件中指示的 oci:dbname=//localhost:1521/orcl (host:port/service_name)。使用 SQL developer 确保连接字符串的正确性。

you may check the below link, this link illustrates the matching of listener.ora and connection string parameters and there is php pdo code snippet at the end with the correct usage of the connection string.

你可以查看下面的链接,这个链接说明了listener.ora和连接字符串参数的匹配,最后有php pdo代码片段,正确使用了连接字符串。

https://www.youtube.com/watch?v=pMQXVihgrrE

https://www.youtube.com/watch?v=pMQXVihgrrE

https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/

https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/

回答by Michael P.

Wrong, Wrong & Wrong.

错,错与错。

PHPinfo() will NOT enable the PDO driver nor will it show up.

PHPinfo() 不会启用 PDO 驱动程序,也不会显示。

You do NOT need to download a PDO driver separately the one packaged with your PHP installation will work fine.

您不需要单独下载 PDO 驱动程序,与您的 PHP 安装一起打包的驱动程序可以正常工作。

You do NOT need to install the instant client as your PHP for windows will have the instant client built-in.

您不需要安装即时客户端,因为 Windows 版 PHP 将内置即时客户端。

Solution: Updating IIS7 with PHP manager or updating the PHP ini file within your installation to Enable the DLL.

解决方案:使用 PHP 管理器更新 IIS7 或更新安装中的 PHP ini 文件以启用 DLL。

extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_sqlsrv.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll

Its all about the DLL's folks!

一切都与 DLL 的人有关!

@Shailesh Sonare thanks for that great PDO connection class

@Shailesh Sonare 感谢您提供出色的 PDO 连接类

ALSO PLEASE NOTE: PDO will soon be DEPRECATED Oracle recommends OCI8 so you shouldn't be using PDO anyway! ** edit outdated manual but answer is still correct!

另请注意:PDO 将很快被弃用 Oracle 建议使用 OCI8,因此无论如何您都不应该使用 PDO!** 编辑过时的手册,但答案仍然正确!