2
      CREATE OR REPLACE PACKAGE BODY simpleState IS

      PROCEDURE selectFromTable(tableName VARCHAR2, columnName VARCHAR2) IS
      TYPE c1 IS TABLE OF VARCHAR2(30);
      Notes c1;
      BEGIN
        EXECUTE IMMEDIATE 'Select ' || columnName || ' FROM ' || tableName BULK COLLECT INTO Notes;
        FOR idx IN Notes.FIRST .. Notes.LAST LOOP
          DBMS_OUTPUT.PUT_LINE(Notes(idx));
        END LOOP;
      END selectFromTable;


    $conn = oci_connect("student", "STUDENT", "localhost");

    if (!$conn) {
        $m = oci_error();
        echo $m['message'], "\n";
        exit;
    }

else {
    print "Connected to Oracle!";
}


    if(isset($_POST["readSubmit"])){

        $table = $_POST['ReadTableName'];
        $column = $_POST['ReadColumn'];

        $stid = oci_parse($conn, 'begin simpleState.selectFromTable(:a,:b); end;');

        ocibindbyname($stid, 'a', $table);
        ocibindbyname($stid, 'b', $column);

        if (!$stid) {
            $e = oci_error($conn);
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }


            $r = oci_execute($stid);
            echo "$r";
        oci_free_statement($stid);
        oci_close($conn);    
        }   

        ?>

I am trying to display the message in the DBMS_OUPUT.PUT_LINE in the browser using PHP. But it doesn't display anything. The PL/SQL code works and displays what it has to display in SQL Developer. Can anyone help me with this?

4

2 回答 2

2

I had the same problem few days ago but I was lazy and I stopped searching for a solution, but, I found a solution now.

Instead a procedure, I've created a function that returns a SYS_REFCURSOR.

CREATE OR REPLACE FUNCTION selectFromTable(tableName VARCHAR2, columnName VARCHAR2) 
  RETURN SYS_REFCURSOR 
AS
  my_cursor SYS_REFCURSOR;
  s_query varchar2(500);
BEGIN

  s_query := 'SELECT ' || columnName || ' FROM ' || tableName;

  OPEN my_cursor FOR s_query;

  RETURN my_cursor;
END selectFromTable;
/

PHP code is pretty simple.

<?php
   $conn = oci_connect('student', 'STUDENT', 'localhost/XE');
   if (!$conn) {
      $e = oci_error();
      trigger_error(htmlentities($e('message'), ENT_QUOTES), E_USER_ERROR);
   }

   $tablename = 'STUDENTI';
   $columnname = 'NR_MATRICOL';

   $query = "begin 
               :cursor := selectFromTable(:tabl, :colm);
             end;";

   $stid = oci_parse($conn, $query);

   $p_cursor = oci_new_cursor($conn);

   oci_bind_by_name($stid, ":tabl", $tablename);
   oci_bind_by_name($stid, ":colm", $columnname);

   oci_bind_by_name($stid, ":cursor", $p_cursor, -1, OCI_B_CURSOR);

   oci_execute($stid);
   oci_execute($p_cursor, OCI_DEFAULT);

   while (($row = oci_fetch_array($p_cursor, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
      echo $row['NR_MATRICOL'] . "<br />\n";
   }
?>

Edit: If you need to print more columns, you can just simply add column names here:

$columnname = 'NR_MATRICOL, NUME, PRENUME';

and you need to add echos too:

 echo $row['NR_MATRICOL'] . " - ";
 echo $row['NUME'] . " - ";
 echo $row['PRENUME'] . "<br >";

The output should look like the following:

111 - Popescu - Bogdan
112 - Prelipcean - Radu
123 - Bucur - Andreea
131 - Santa - Claus
于 2016-05-18T05:38:28.303 回答
0

After deep research I found out that i can't actually get something from dbms_output, so I've made a kind of a solution to my problem. I've added an OUT parameter to my PL/SQL procedure, where I've concatenated all my results. I've added the chr(10) to have newlines and simulate different row results in oracle.

so I've added PROCEDURE selectFromTable(tableName VARCHAR2, columnName VARCHAR2, p_out OUT VARCHAR2); and modified DBMS_OUTPUT.PUT_LINE(Notes(idx)); into p_out := p_out || Notes(idx) || chr(10);. Also I've added anoter :c variabile to my PHP function and added ocibindbyname($stid, 'c', $outVal, 300); for it.

于 2016-05-17T19:22:01.477 回答