如何向oracle数据库发送值?

时间:2018-05-01 04:18:17

标签: php oracle

我需要向第三方(Oracle数据库)发送一个发票号码,他们会回复我,同时我尝试与他们建立连接,我得到了一个成功的结果但是现在我需要发送一个SQL查询以获得响应,但不幸的是我不知道如何发送。 这是我使用的连接和查询代码: -

<?php
  $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 83.111.41.30)(PORT = 1536)))(CONNECT_DATA=(SID=TEST)))" ;

  if($c = OCILogon("XXWEBSITE", "xxwebsite", $db))
  {
    echo 'success';
    $query = "BEGIN 
      P_INV_NUMBER := '34499';
      P_INV_NUM := NULL;
      P_UNIT_NO := NULL;
      P_CUST_NAME := NULL;
      P_INV_DATE := NULL;
      P_TRANS_TYPE := NULL;
      P_CUST_NO := NULL;
      P_OUTSTANDING_AMOUNT := NULL;
      L_ERROR := NULL;
      APPS.XX_AR_CUST_OS_BAL_PKG.XX_AR_CUST_OS_BAL_PROC ( P_INV_NUMBER, P_INV_NUM, P_UNIT_NO, P_CUST_NAME, P_INV_DATE, P_TRANS_TYPE, P_CUST_NO, P_OUTSTANDING_AMOUNT, L_ERROR );
      COMMIT; 
    END;";
      $stid = oci_parse($c, $query);
      $check =  oci_execute($stid);
 print_r($check);
       OCILogoff($c);
     }
  else
  {
      $err = OCIError();
      echo "Connection failed.";
  }
?> 

他们要求发送的查询是: -

DECLARE 
      P_INV_NUMBER VARCHAR2(32767);
      P_INV_NUM VARCHAR2(32767);
      P_UNIT_NO VARCHAR2(32767);
      P_CUST_NAME VARCHAR2(32767);
      P_INV_DATE DATE;
      P_TRANS_TYPE VARCHAR2(32767);
      P_CUST_NO NUMBER;
      P_OUTSTANDING_AMOUNT NUMBER;
      L_ERROR VARCHAR2(32767);
    BEGIN 
      P_INV_NUMBER := '34499';
      P_INV_NUM := NULL;
      P_UNIT_NO := NULL;
      P_CUST_NAME := NULL;
      P_INV_DATE := NULL;
      P_TRANS_TYPE := NULL;
      P_CUST_NO := NULL;
      P_OUTSTANDING_AMOUNT := NULL;
      L_ERROR := NULL;
      APPS.XX_AR_CUST_OS_BAL_PKG.XX_AR_CUST_OS_BAL_PROC ( P_INV_NUMBER, P_INV_NUM, P_UNIT_NO, P_CUST_NAME, P_INV_DATE, P_TRANS_TYPE, P_CUST_NO, P_OUTSTANDING_AMOUNT, L_ERROR );
      COMMIT; 
    END;

如何发送这些参数我试过一种方法,但我不知道它是否正确。我需要发送以上内容才能得到结果我该怎么做?

1 个答案:

答案 0 :(得分:3)

最好通过PHP / OCI直接调用存储过程,而不是尝试通过PL / SQL&#34;查询&#34;:

$stid = oci_parse($c, "call apps.xx_ar_cust_os_bal_pkg.xx_ar_cust_os_bal_proc('34499', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)");
oci_execute($stid);

如果需要返回存储过程的结果,则需要使用绑定变量:

$stid = oci_parse($c, "call apps.xx_ar_cust_os_bal_pkg.xx_ar_cust_os_bal_proc('34499', :inv_num, :unit_no, :cust_name, :inv_date, :trans_type, :cust_no, :outstanding_amount, :err)");
/* Do this for each bind variable */
oci_bind_by_name($stid, ":inv_num", $inv_num);
...
oci_execute($stid);

编辑:将P_INV_NUMBER参数作为绑定变量传递:

$stid = oci_parse($c, "call apps.xx_ar_cust_os_bal_pkg.xx_ar_cust_os_bal_proc(:inv_number, :inv_num, :unit_no, :cust_name, :inv_date, :trans_type, :cust_no, :outstanding_amount, :err)");
oci_bind_by_name($stid, ":inv_number", "34499");
/* Do this for each bind variable */
oci_bind_by_name($stid, ":inv_num", $inv_num);
...
oci_execute($stid);

希望这有帮助。