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?

5
  • 1
    Why are you trying to do this? DBMS_OUTPUT is something that's used in general only for adhoc messages - eg. for testing purposes - rather than a method of passing data from one system to another. If I were you, I'd investigate ref cursors - this is the preferred solution for passing data from select statements back to the calling code. Commented May 17, 2016 at 19:09
  • Well, needed a fast solution to integrate a pl/sql procedure with an user interface (something simple like a html page) Commented May 17, 2016 at 19:27
  • Oh and I've already tried ref cursors.... Commented May 17, 2016 at 19:29
  • Were you not able to get the ref cursor method to work? Commented May 17, 2016 at 19:30
  • Yes, and I don't have the code where I've tried (deleted it to trysomething else) Commented May 17, 2016 at 19:42

2 Answers 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
Sign up to request clarification or add additional context in comments.

2 Comments

I've alreadt tried with ref cursors, I'll try your solution also. Thank you!
Worked perfectly! Thank you!
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.

5 Comments

As a note to this solution, chr(10) works only in SQL Developer... in PHP it doesen't display it as a new line unfortunately.
Perhaps you need chr(13)||chr(10) instead of just chr(10)?
In SQL Developer chr(10) is enough, I've tried also with chr(13)||chr(10), as you've said but it's the same in PHP (all the results are displayed in one line)
That's because your browser renders it as HTML. Newlines mean nothing in HTML.
I've thought about that, just hoped it'll work if i'll pass it from oracle through php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.