连接到oracle:使用PDO工作但不使用oci_connect

时间:2016-07-15 12:33:49

标签: php oracle pdo oracle11g

工作代码 -

$dbh = new PDO('oci:dbname=localhost/XE', 'hr', 'hr');
$s = $dbh->prepare("select * from Employees");
$s->execute();
while (($r = $s->fetch(PDO::FETCH_ASSOC)) != false) {
    echo htmlentities($r['FIRST_NAME']) . " " . $r['LAST_NAME'] . "<br>";
}

无效代码 (错误调用未定义函数oci_connect(),oci_new_connect())

/*Using oci_connect */
$conn=oci_connect("hr","hr","localhost/XE");
If (!$conn)
    echo 'Failed to connect to Oracle';
else 
    echo 'Succesfully connected with Oracle DB';

oci_close($conn);

/*Using oci_new_connect*/

$c = oci_new_connect('hr', 'hr', 'localhost/XE');
$s = oci_parse($c, 'select city from locations');
oci_execute($s);
while (($res = oci_fetch_array($s, OCI_ASSOC)) != false) {
 echo htmlentities($res['CITY']) . "<br>";
}

phpinfo为phpinfo details 我已经在c中放置了11g的instantclient并设置了PATH变量

逐个尝试每个即时客户端扩展程序 -

  1. extension = php_oci8.dll,
  2. 延长= php_oci8_11g.dll,
  3. 延长= php_oci8_12c.dll
  4. 我正在使用Oracle 11g

    还试过 - thisthis also

1 个答案:

答案 0 :(得分:0)

OCI_CONNECT需要服务器上的TNSNAMES.ORA文件。主机名(作为IP)仅在域控制器注册名称时有效。

      <?php
      $oracleHost="IP or Hostname";
      $oracleLogin="username";
      $oraclePassword="passworrd";
      $oraclePort="1521";
      $oracleSid="sidname";
      $tns="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$oracleHost)(PORT=$oraclePort))(CONNECT_DATA=(SID=$oracleSid)))";$oracleConnect = oci_connect($oracleLogin,$oraclePassword, $tns);
      ?>