记Windows环境下PHP7.2配置连接Oracle过程

  因为项目需求,需要去项目的Oracle数据库定时获取部分数据,在本地Windows环境配置连接Oracle的方法,一边百度,一边操作,碰到一个问题卡了一天,在这里记录分享下处理方法,希望对大家有所帮助。

  1. 安装Oracle Instant Client
  从oracle官方网站下载instant client文件,链接:https://www.oracle.com/database/technologies/instant-client/downloads.html,可根据自己电脑环境选择对应版本下载,php.ini中会有说明需要安装哪个版本的client:

    

    instantclient-basic-nt-12.2.0.1.0.zip
    instantclient-jdbc-nt-12.2.0.1.0.zip
    instantclient-sqlplus-nt-12.2.0.1.0.zip
    instantclient-tools-nt-12.2.0.1.0.zip
  下载以上文件(版本可根据自己需求下载)并将以上zip文件解压到同一个目录下,便于查找和使用,如:D:\oracle\instantclient_12_2,以下均已此路径作为示例。

  2. 配置系统环境变量

    TNS_ADMIN=D:\oracle\instantclient_12_2\network\admin
    PATH=D:\oracle\instantclient_12_2
    ORACLE_HOME=D:\oracle\instantclient_12_2
    LD_LIBRARY_PATH=D:\oracle\instantclient_12_2

  3. 在php.ini中打开oci扩展或者pdo_oci扩展

    

 

  4. 下载oci8扩展,链接http://pecl.php.net/package/oci8,根据php版本选择oci8的版本。

    

 

   下载解压后将php_oci8.dll和php_oci8_12c.dll拷贝到PHP的ext目录中

  5. 将上面下载的D:\oracle\instantclient_12_2中的oci.dll、oraocci12.dll拷贝到PHP安装目录和C:/windows/system32中

  6. 然后重启电脑,测试链接情况

<?php
//echo phpinfo();
ini_set('display_errors', 1);
error_reporting(E_ALL);

// 注意连接地址、端口、账号、密码改成自己的
$config = array ( 'oracleDbConfig' => array ( 'connect_string' => '(DEscriptION=(ADDRESS=(PROTOCOL =TCP)(HOST=127.0.0.1)(PORT = 1521))(CONNECT_DATA =(SID=ORCLCDB)))', 'port' => '1521', 'user' => 'SYNC', 'password' => 'databaseOracle', ), ); //返回值 $arr_result = array(); $arr_result['result'] = 'false'; //true false 为黑名单 $arr_result['callerid'] = []; //取数据库参数 $db_host_name = $config['oracleDbConfig']['connect_string']; $db_user_name = $config['oracleDbConfig']['user']; $db_pwd = $config['oracleDbConfig']['password']; //连接Oracle $conn = oci_connect($db_user_name, $db_pwd, $db_host_name); if (!$conn) {// 连接错误 $e = oci_error(); echo "连接Oracle时出错,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.") ".$e['message']; $arr_result['result'] = 'false'; return false; } else { echo("连接成功!"); $select = "SELECT * FROM all_users"; $result_rows = oci_parse($conn, $select); // 配置SQL语句,执行SQL $execute_result = oci_execute($result_rows); if (!$execute_result) { //没有行 $e = oci_error($result_rows); echo "查询时出错或没有行!,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.") ". $select." ".$e['message']; $arr_result['result'] = 'false'; var_dump($arr_result); //默认为不是黑名单 }else{ while ($row = oci_fetch_assoc($result_rows)) { echo "username:"; print_r($row['USERNAME']); echo "<br>"; } } }

  7. 运行此脚本,报错:Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that PATH includes the directory with Oracle Instant Client libraries in F:\CCode\bx\standard\test.php on line 25

  查询了很多资料,最终发现,需要将D:\oracle\instantclient_12_2中的oraociei12.dll也拷贝到php安装目录中,运行成功。

 

posted @ 2022-05-11 15:52  vlson  阅读(564)  评论(0编辑  收藏  举报