将 PDO 与 Oracle 数据库连接 [英] Connect PDO with Oracle database

查看:59
本文介绍了将 PDO 与 Oracle 数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Username/Connection/Database = CustomSearch
Table = Reservation_General_2

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

There are some columns inside that table and some data. but the point is I cant connect to 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.

错误:找不到驱动

这些是我从 Oracle Sql Developer 连接时的连接设置.

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

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

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

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

I added the driver by removing semicolon from the php.ini file

extension=php_pdo_oci.dll 

但我开始收到此错误.

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

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

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

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

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

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

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 当前不知道在连接描述符 (ext\pdo_oci\oci_driver.c:635) 中请求的实例

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)

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

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

我试图按照 Stack Overflow 上的这个答案来创建连接字符串.

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

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

更新 2

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

Update 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?

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

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

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

我收到消息已连接到数据库",这意味着 echo 有效,因为 PDO 没有给出错误.

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

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

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?

推荐答案

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

Check PDO and OCI drivers installed properly or not

试试下面的代码

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

这篇关于将 PDO 与 Oracle 数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆