<html>
<! QueryPhotoPLSQL.php                                        >
<! Chapter 15, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This script demonstrates how to return BFILE into a web    >
<! by using a PL/SQL stored procedure.                        >
<head>
<title>
  Chapter 15: QueryPhotoPLSQL.php
</title>
<style>
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
</style>
</head>
<body>
<?php
  // Return successful attempt to connect to the database.
  if ($c = @oci_connect("php","php","xe"))
  {
    // Declare input variables.
    (isset($_GET['id'])) ? $id = (int) $_GET['id'] : $id = 1;
    (isset($_GET['name'])) ? $name = $_GET['name'] : $name = "Washington";

    // Declare a PL/SQL execution command.
    $stmt = "BEGIN biography.view_photograph(:id,:alias,:f_name); END;";

    // Parse a query through the connection.
    $s = oci_parse($c,$stmt);

    // Bind PHP variables to the OCI types.
    oci_bind_by_name($s,':id',$id,-1,SQLT_INT);

    // Set OUT mode procedure variables physical runtime size.
    // Avoids an ORA-21560: argument 2 is null, invalid, or out of range
    oci_bind_by_name($s,':alias',$alias,255,SQLT_CHR);
    oci_bind_by_name($s,':f_name',$f_name,255,SQLT_CHR);

    // Execute the PL/SQL statement.
    if (oci_execute($s,OCI_DEFAULT))
    {
      // Format HTML table to display photograph.
      $out = '<table border="1" cellpadding="3" cellspacing="0">';
      $out .= '<tr>';
      $out .= '<td align="center" class="e">Photo of '.$name.'</td>';
      $out .= '</tr>';
      $out .= '<tr>';
      $out .= '<td align="center" class="v" valign="center">';
      if (!is_null($f_name))
        $out .= '<img src="/mydirectory/'.$f_name.'">';
      else
        $out .= 'No available photo';
      $out .= '</td>';
      $out .= '</tr>';
      $out .= '</table>';
    }

    // Print the HTML table.
    print $out;

    // Disconnect from database.
    oci_close($c);
  }
  else
  {
    // Assign the OCI error and format double and single quotes.
    $errorMessage = oci_error();
    print htmlentities($errorMessage['message'])."<br />";
  }
?>
</body>
</html>