首页 > 解决方案 > 如何从 oci_execute() 获得结果?

问题描述

在此处输入图像描述

在此处输入图像描述

它给出了 true 但是当我使用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);
}

标签: phporacle

解决方案


正如我在评论部分告诉你的那样,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', '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);

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

?>

在您的情况下,您正在执行一个 PROCEDURE,它提供用户定义的类型作为输出。在这种情况下,您可能会尝试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);

?>

推荐阅读