如何从oci_execute获得结果?

如何解决如何从oci_execute获得结果?

enter image description here enter image description here 它为真,但是当我使用oci_fetch($stmt)时会显示错误。

oci_fetch():ORA-24374:在获取或执行之前未完成定义,并且 提取

$sql = "DECLARE
            C1  KTI_OPPL_DB.MH_ONLINE_PACKAGE_DB.TABLE_OF_LOV;
        BEGIN
        KTI_OPPL_DB.MH_ONLINE_PACKAGE_DB.GET_VESSEL_TYPE_LOV(C1);
          
         
        END;";
$stmt = oci_parse($conn,$sql);
$r = oci_execute($stmt);
   
while (oci_fetch($stmt)) {
    $nrows = oci_num_rows($stmt);
}

解决方法

正如我在注释部分中告诉您的那样,oci_fetch将不会提供任何结果,因为您正在执行的语句不是sql语句,而是pl / sql过程。

如何使用OCI_FETCH

将查询的下一行提取到内部缓冲区中,这些缓冲区可以通过oci_result()或通过先前使用oci_define_by_name()定义的变量进行访问。

使用oci_result

的示例
<?php

$conn = oci_connect('hr','welcome','localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'],ENT_QUOTES),E_USER_ERROR);
}

$sql = 'SELECT location_id,city FROM locations WHERE location_id < 1200';
$stid = oci_parse($conn,$sql);
oci_execute($stid);

while (oci_fetch($stid)) {
    echo oci_result($stid,'LOCATION_ID') . " is ";
    echo oci_result($stid,'CITY') . "<br>\n";
}

// Displays:
//   1000 is Roma
//   1100 is Venice

oci_free_statement($stid);
oci_close($conn);

?>

oci_define_by_name

的示例
<?php

$conn = oci_connect('hr',$sql);

// The defines MUST be done before executing
oci_define_by_name($stid,'LOCATION_ID',$locid);
oci_define_by_name($stid,'CITY',$city);

oci_execute($stid);

// Each fetch populates the previously defined variables with the next row's data
while (oci_fetch($stid)) {
    echo "Location id $locid is $city<br>\n";
}

// Displays:
//   Location id 1000 is Roma
//   Location id 1100 is Venice

oci_free_statement($stid);
oci_close($conn);

?>

在您的情况下,您正在执行一个过程,该过程提供了用户定义的类型作为输出。在这种情况下,您可以尝试oci_fetch_array来获取过程的结果,该过程以三个值的数组的形式传递(这是从输出中获得的值)。 PHP和Oracle用户定义的类型比较棘手,因此,我会尝试使用此方法(适应您的代码):

<?php
  

$stid = oci_parse($conn,'BEGIN yourprocedure(:rc); END;');
$refcur = oci_new_cursor($conn);
oci_bind_by_name($stid,':rc',$refcur,-1,OCI_B_CURSOR);
oci_execute($stid);

// Execute the returned REF CURSOR and fetch from it like a statement identifier
oci_execute($refcur);  
echo "<table border='1'>\n";
while (($row = oci_fetch_array($refcur,OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "    <td>".($item !== null ? htmlentities($item,ENT_QUOTES) : "&nbsp;")."</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

oci_free_statement($refcur);
oci_free_statement($stid);
oci_close($conn);

?>
,

在不知道创建您的类型的确切PL / SQL的情况下,我们只能猜测TABLE_OF_LOV是什么。这是一个示例,显示了从TABLE OF VARCHAR2获取记录,这似乎是可行的猜测。

<?php

error_reporting(E_ALL); 
ini_set('display_errors','On');

$c = oci_connect("hr","welcome","localhost/XE");
if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'],E_USER_ERROR);
}

//
// Create a PL/SQL package that has a 'TABLE OF' OUT parameter
//

$create_pkg = "
    CREATE OR REPLACE PACKAGE mypackage AS
        TYPE TABLE_OF_LOV IS TABLE OF VARCHAR(20) INDEX BY BINARY_INTEGER;
        PROCEDURE GET_VESSEL_TYPE_LOV(p1 OUT TABLE_OF_LOV);
    END mypackage;";
$s = oci_parse($c,$create_pkg);
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'],E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'],E_USER_ERROR);
}

$create_pkg_body = "
    CREATE OR REPLACE PACKAGE BODY mypackage AS
        PROCEDURE GET_VESSEL_TYPE_LOV(p1 OUT TABLE_OF_LOV) IS
        BEGIN
            p1(1) := 'one';
            p1(2) := 'two';
            p1(3) := '';
            p1(4) := 'four';
            p1(5) := 'five';
        END GET_VESSEL_TYPE_LOV;
    END mypackage;";
$s = oci_parse($c,$create_pkg_body);
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'],E_USER_ERROR);
}

//
// Call the PL/SQL procedure
//

$s = oci_parse($c,"BEGIN mypackage.get_vessel_type_lov(:bv); END;");
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'],E_USER_ERROR);
}

$r = oci_bind_array_by_name($s,":bv",$array,5,20,SQLT_CHR);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not bind a parameter: '. $m['message'],E_USER_ERROR);

}
$r = oci_execute($s);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'],E_USER_ERROR);
}

var_dump($array);

?>

输出为:

$ php so3.php 
array(5) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(0) ""
  [3]=>
  string(4) "four"
  [4]=>
  string(4) "five"
}

您可能会找到其他解决方案,例如在免费书籍The Underground PHP and Oracle Manual的p187的将PL / SQL与OCI8一起使用一章中编写“ PL / SQL包装器”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-