如何从PHP中的plsql调用带有参数的函数?

时间:2019-05-29 19:06:08

标签: php plsql

我试图用PHP中的plsql调用带有参数的函数,但它不起作用,但是我不知道如何。

我的问题是我在尝试存储函数$sqlGetGenre的地方有一个变量,例如,我不知道如何使用$param参数调用该函数。

此外,我无法解决的另一件事是在另一个函数中调用一个函数。我已经在Google上搜索,但是找不到任何东西。

创建存储过程

$sqlGetGenre = "CREATE OR REPLACE FUNCTION getGenre(bookISBN IN VARCHAR2) RETURN VARCHAR2"
."AS"
."v_bookGenre VARCHAR2(100);"
."BEGIN"
."select genre into v_bookGenre from books where bookISBN=isbn;"
."return v_bookGenre;"

."EXCEPTION"
."when no_data_found then"
."return null;"
."END getGenre;";
$getGenre = oci_parse($conn, $sqlGetGenre);
oci_execute($getGenre);

创建存储过程


$sqlGetTitle ="CREATE OR REPLACE FUNCTION getTitle(bookISBN IN VARCHAR2) RETURN VARCHAR2"
."AS"
."v_bookTitle VARCHAR2(100);"
."BEGIN"
."select title into v_bookTitle from books where bookISBN=isbn;"

."return v_bookTitle;"

."EXCEPTION"
."when no_data_found then"
."return null;"
."END getTitle;";
$getTitle = oci_parse($conn, $sqlgetTitle);
   oci_execute($getTitle);

   $sqlCreateType1 = "CREATE OR REPLACE TYPE t IS TABLE OF varchar2(200);";
   $createType1 = oci_parse($conn, $sqlCreateType1);
   oci_execute($createType1);


   $sqlCreateType2 = "CREATE OR REPLACE TYPE typeRecom IS VARRAY(200) of VARCHAR2(200);";
   $createType2 = oci_parse($conn,$sqlCreateType2);
   oci_execute($createType2);
       $sqlTopRecommandations = "CREATE OR REPLACE FUNCTION topRecommandations(idUser IN VARCHAR2)" 
      ."RETURN typeRecom"
      ."AS"

     ."lastGenres t :=t();"
     ."readedBooks t:=t();"
     ."topTen typeRecom:=typeRecom();"

     ."v_i NUMBER := 1;"
     ."v_j NUMBER := 1;"

    ."verifDateBook NUMBER:=0;"
    ."verifDateLoan NUMBER:=0;"

    ."BEGIN"
    ."select count(*) into verifDateBook from books;"
   ."if(verifDateBook = 0) then"
   ."raise no_data_found;"
   ."end if;"
   ."select count(*) into verifDateLoan from loans;"
   ."if(verifDateLoan = 0) then"
   ."raise no_data_found;"
   ."end if;"

   ."lastGenres.extend();"
  ."for i in (select genre from books join loans on books.isbn=loans.bookId where loanDate>add_months(sysdate,-6) and idUser=loans.regNo order by genre desc) loop"
  ."lastGenres.extend();"
  ."lastGenres(lastGenres.count) := i.genre;"
  ."end loop;"
  ."readedBooks.extend();"
  ."for i in (select bookId from loans group by bookId order by count(bookId) desc) loop"
  ."readedBooks.extend();"
  ."readedBooks(readedBooks.count) := i.bookId;"
  ."end loop;"
  ."--select bookId into readedBooks from loan order by count(bookId) desc;"

  ."for v_i IN 1..readedBooks.count LOOP"
  ."for v_j in 1..lastGenres.count LOOP"

  ."if(lastGenres(v_j)=getGenre(readedBooks(v_i))) then"
  ."topTen.extend();"
  ."topTen(topTen.count) := getTitle(readedBooks(v_i));"
  ."end if;"
  ."EXIT WHEN topTen.count=10;"
  ."END LOOP;"
  ."EXIT WHEN topTen.count=10;"
  ."end LOOP;"
 ."return topTen;"
 ."EXCEPTION"
 ."WHEN no_data_found THEN"
 ."return null;"

."END topRecommandations;";

$topRecommandations = oci_parse($conn, $sqlTopRecommandations);
oci_execute($topRecommandations);

执行topRecommandations

$s = oci_parse($conn, "begin :ret :=topRecommandations(:param1); end;");
oci_bind_by_name($s, ':ret', $r, 200);
oci_bind_by_name($s, ':param1', $_SESSION['RollNo']);
oci_execute($s);
echo "Result is: ".$r;

1 个答案:

答案 0 :(得分:0)

您可以调用一个函数,例如这样:

$s = oci_parse($c, "begin :ret :=getGenre(:param1); end;");
oci_bind_by_name($s, ':ret', $r, 200);
oci_bind_by_name($s, ':param1', $value1);
oci_execute($s);
echo "Result is: ".$r;

如果您的存储过程没有参数,那么您可以用相同的方式调用它,只需删除参数值

$s = oci_parse($c, "begin :ret :=recommandation(); end;");
oci_bind_array_by_name($s,":ret",$arr,100,-1,SQLT_CHR);
oci_execute($s);