在oracle数据库和php代码之间创建连接时遇到的问题

oalqel3c  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(138)

你好,我正在开发一个PHP Web应用程序。不幸的是,我对Oracle DB不太适应。所以我有一个数据库的TNS条目。类似于:

DBNAME =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
    (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
    (LOAD_BALANCE = yes)
    (FAILOVER = yes)
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = dbname)
      (FAILOVER_MODE =
        (TYPE = SELECT)
        (METHOD = BASIC)
        (RETRIES = 25)
        (DELAY = 5)
      )
    )
  )

我得到了这个
jdbc:oracle:thin@//xxxxxxxxxxx.com:1530/dbname
我试过了,但没有用:

<?php error_reporting(E_ALL); ?>

<?php
class PDOConnection {
    private $db;

    function _construct(){
    try{
        $server = 'xxxxxxxxxxx.com';
        $username = "xxxxx";
        $password = "xxxxx";
        $service_name = "dbname";
        $sid = '';
        $port = 1530;

        $dbCon ="DBNAME =
        (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
        (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
        (LOAD_BALANCE = yes)
        (FAILOVER = yes)
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = dbname)
          (FAILOVER_MODE =
            (TYPE = SELECT)
            (METHOD = BASIC)
            (RETRIES = 25)
            (DELAY = 5)
          )
        )
      )";
      $this->db = new PDO("oci:dbname=" . $dbCon."; charset=utf8", $username, $password,[
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
      ]);
      echo "Connected";
    } catch(PDOException $e){
        echo $e->getMessage();
    }

    }
}
$obj = new PDOConnection();
?>

我也试过这个:

<?PHP
$oci_user = "xxxxx";
$oci_psw = "xxxxx";
$oci_sid = "sid";
$db_charset = 'utf-8'
?>

我做了很多研究,但我无法创建与我的oracle数据库的连接。你能帮我吗?
如何在我的PHP代码和oracle数据库之间创建连接。
debugging :
dbConn.php文件:

<?php

class dbConn
{

    private $db;       //The db handle
    public  $num_rows; //Number of rows
    public  $last_id;  //Last insert id
    public  $aff_rows; //Affected rows
    private $stid;     //parsed command

    public function __construct($dbuser, $dbpassword, $db_connection)
    {
        $this->db = oci_connect ($dbuser, $dbpassword, $db_connection);
        if (!$this->db) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
    }

    public function close()
    {
        oci_close($this->db);
    }

    public function execute($sql)
    {
        $this->stid = oci_parse ($this->db, $sql);
        $rst = oci_execute ($this->stid);
        $rows=array();
        $this->aff_rows = 0;
        if ($rst){
            $this->aff_rows= oci_fetch_all ($this->stid, $rows);
        }
        oci_free_statement($this->stid);
        return $rows;
    }
}
?>

test.php文件:

<?php include 'dbConn.php'; 
$connstr= "DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxxxx.com)(PORT = 1530))
(ADDRESS = (PROTOCOL = TCP)(HOST = xxxxx.com)(PORT = 1530))
(LOAD_BALANCE = yes)
(FAILOVER = yes)
(CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = xxxxx)
  (FAILOVER_MODE =
    (TYPE = SELECT)
    (METHOD = BASIC)
    (RETRIES = 25)
    (DELAY = 5)
  )
)
)";

mydb = new dbConn ($username, $userpassw, $connstr);
?>

错误数:

Warning: Undefined variable $username in test.php on line 19

Warning: Undefined variable $userpassw in test.php on line 19

Fatal error: Uncaught Error: Call to undefined function oci_connect() in dbConn.php:14 Stack trace: #0 
test.php(19): dbConn->__construct(NULL, NULL, 'DESCRIPTION =\r\n...') #1 {main} thrown in dbConn.php on line 14
dojqjjoe

dojqjjoe1#

我没有使用PDO的经验,但我使用oc_Oracle的方式如下:在$db_connection中提供完整TNS条目,如下所示:PHP页面:

require 'dbConn.php';
$connstr= "(DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
        (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxxxxxxx.com)(PORT = 1530))
        (LOAD_BALANCE = yes)
        (FAILOVER = yes)
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = dbname)
          (FAILOVER_MODE =
            (TYPE = SELECT)
            (METHOD = BASIC)
            (RETRIES = 25)
            (DELAY = 5)
          )
        )
      )";

$mydb = new dbConn ($username, $userpassw, $connstr);

在dbConn.php中:

class dbConn
{

    private $db;       //The db handle
    public  $num_rows; //Number of rows
    public  $last_id;  //Last insert id
    public  $aff_rows; //Affected rows
    private $stid;     //parsed command

    public function __construct($dbuser, $dbpassword, $db_connection)
    {
        $this->db = oci_connect ($dbuser, $dbpassword, $db_connection);
        if (!$this->db) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
    }

    public function close()
    {
        oci_close($this->db);
    }

    public function execute($sql)
    {
        $this->stid = oci_parse ($this->db, $sql);
        $rst = oci_execute ($this->stid);
        $rows=array();
        $this->aff_rows = 0;
        if ($rst){
            $this->aff_rows= oci_fetch_all ($this->stid, $rows);
        }
        oci_free_statement($this->stid);
        return $rows;
    }
}

相关问题