3 Replies Latest reply: Mar 29, 2011 2:25 PM by FlyingGuy RSS

    PHP and serveroutput

    202405
      Is there a way for a stored procedure that uses DBMS_OUTPUT to send the output to the web browser?

      TIA
        • 1. Re: PHP and serveroutput
          3047
          Here is some simple (aka inefficient) code that may help.
          Use it at your own risk.

          The bind size for ":LN" may need to be increased for some character sets.

          -- CJ
          <?php
          
          // Turn dbms_output ON or OFF
          function SetServerOutput($con, $p)
          {
            if ($p)
              $s = "BEGIN DBMS_OUTPUT.ENABLE(1000000); END;";
            else
              $s = "BEGIN DBMS_OUTPUT.DISABLE(); END;";
          
            $r = false;
            $stid = doParse($con, $s);
            if ($stid) {
              $r = doExecute($stid);
              @OCIFreeStatement($stid);
            }
            return $r;
          }
          
          // Fetch and display any dbms_output
          function DisplayDbmsOutput($con)
          {
            $r = GetDbmsOutput($con);
          
            if (!$r)
              print "<p>No dbms_output</p>\n";
            else
              foreach ($r as $line)
                echo $line."<br>\n";
          }
          
          // Returns an array of dbms_output lines, or false.
          function GetDbmsOutput($con)
          {
            $res = false;
            $stid = doParse($con, "BEGIN DBMS_OUTPUT.GET_LINE(:LN, :ST); END;");
            if ($stid) {
              if (doBind($stid, ":LN", $ln, 255) &&
                  doBind($stid, ":ST", $st, "")) {
                $res = array();
                while ($succ = doExecute($stid)) {
                  if ($st)
                    break;
                  $res[] = $ln;
                }
                if (!$succ)
                  $res = false;
              }
              @OCIFreeStatement($stid);
            }
            return ($res);
          }
          
          // Parse
          function doParse($con, $stmt)
          {
            $stid = @OCIParse($con, $stmt);
            if (!$stid)
              PrintOCIError(@OCIError($con));
            return ($stid);
          }
          
          // Bind
          function doBind($stid, $bn, &$bv, $ln)
          {
            $s = @OCIBindByName($stid, $bn, $bv, $ln);
            if (!$s)
              PrintOCIError(@OCIError($stid));
            return ($s);
          }
          
          // Execute
          function doExecute($stid)
          {
            $s = @OCIExecute($stid);
            if (!$s)
              PrintOCIError(@OCIError($stid));
            return ($s);
          }
          
          // Display OCI error
          function PrintOCIError($err)
          {
            echo "<p><b>Error</b>:</p>\n<pre>".htmlentities($err['message'])."</pre>\n";
          }
          
          // MAIN
          
          echo "<p>Connecting . . . </p>";
          $con = @OCILogon("scott", "tiger", "");
          if (!$con) {
            PrintOCIError(@OCIError());
            die();
          }
          
          // turn serveroutput on
          SetServerOutput($con, true);
          
          // Create dbms_output
          $s = doParse($con, "begin dbms_output.put_line('Hi'); end;");
          if ($s)
            doExecute($s);
          
          // Display the output
          DisplayDbmsOutput($con);
          ?>
          • 2. Re: PHP and serveroutput
            825842
            docjones wrote:

            Is there a way for a stored procedure that uses DBMS_<font face="tahoma,verdana,sans-serif" size="1" color="#000">OUTPUT</font> to send the output to the web browser?



            TIA
            Can you explain it more clearly? I've no clear idea about your words, I'm new to this.
            • 3. Re: PHP and serveroutput
              FlyingGuy
              I am replying for CJ here. My words not his, but....

              We really don;t have the time to teach you PHP. What he has done for you is fairly straight ahead PHP. Please read through it carefully. If you don;t understand the whole thing break it down line by line if you have to. Between the PHP documentation and the "Underground oracle / PHP book that is free it should become clear. Please mark this question is answered.