<!DOCTYPE html>
<html>

<head>
    <title> Connecting PHP to Oracle - and demo if varchar2 
            problem still there 
    </title>

    <link href="tiny-table.css" type="text/css" 
          rel="stylesheet" />

    <meta charset="utf-8" />
</head>

<body>
    <h1> Connecting PHP to Oracle 2013 - 
         demo if varchar2 still a problem </h1>

    <?php
        // do you need to ask for username and password?

        if (! array_key_exists('username', $_POST))
        {
            // using a HERE doc here 

            print <<<PASSWORD_FORM
    <h2> Log in </h2>
    <form method="post" action="$_SERVER[PHP_SELF]">
PASSWORD_FORM;
    ?>
        <label> Username: 
                <input type="text" name="username" /> </label> 
                <br />

        <label> Password: 
                <input type="password" name="password" /> 
                </label> <br />
        <input type="submit" value="Log in" />
        </form>
    <?php
    } // end of if/screen 1

    else // you HAVE the username and password
    {
    ?>
    <h2> Employee information </h2>

    <?php
        $username = strip_tags($_POST['username']);

        // do we really need to strip/sanitize this...?

        $password = $_POST['password'];

        // try to connect to Oracle student database

        $db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)
                                 (HOST = cedar.humboldt.edu)
                                 (PORT = 1521))
               (CONNECT_DATA = (SID = STUDENT)))";

        $conn = oci_connect($username, $password, $db);

        if ($conn == FALSE)
        {
            print "<p> ACK, try again! couldn't connect! </p>";
            require_once("my-std-footer.html");
            exit;  
        }

        // set up the desired query and execute it

        $stmt = oci_parse($conn, 
                          "select empl_last_name, hiredate, ".
                          "       salary, commission ".
                          "from empl");

        oci_execute($stmt, OCI_DEFAULT);
    ?>

    <table>
    <tr> <th> Name </th>
         <th> Date of Hire </th>
         <th> Salary </th>
         <th> Commission </th> </tr>

    <?php
        // iterative through the query results, outputting them

        while (oci_fetch($stmt))
        {
    ?>      
             <tr> 
             <td> <?= oci_result($stmt, "EMPL_LAST_NAME") ?> </td>
             <td> <?= oci_result($stmt, "HIREDATE") ?> </td>
             <td class="right"> $<?= oci_result($stmt, "SALARY") ?> </td>
    <?php
            $curr_commission = oci_result($stmt, "COMMISSION");
	    if ($curr_commission == NULL)
	    {
    ?>
	     <td> (no commission) </td>
    <?php
	    }
            else
            {
    ?>
             <td class="right"> $<?= oci_result($stmt, "COMMISSION") ?> </td>
    <?php
            }
    ?>
             </tr>
    <?php
        }

        // clean up

        oci_free_statement($stmt);

        oci_close($conn);
    ?>
    </table>
    <?php
    } // end of else/screen 2

    // for either screen, if get here, end with standard footer 

    require_once("my-std-footer.html");
    ?>