<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<!--
    FIRST example connecting to Oracle!

    by: Sharon Tuttle
    last modified: 2026-04-01

    you can run this using the URL:
https://nrs-projects.humboldt.edu/~st10/s26cs328/328lect10-2/try-oracle.php
-->

<head>
    <title> Oracle Demo 1 </title>
    <meta charset="utf-8" />

    <?php
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    ?>
    
    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />
</head>

<body>

    <h1> Our First Connection from PHP to Oracle! </h1>

    <?php
    /*===
        using the user directory context information
        to extract the owner-of-this-PHP's username
    ===*/

    $os_username = substr($_SERVER["CONTEXT_PREFIX"], 2);

    /*===
        get this user's Oracle account name, which ends in _php
    ===*/

    $ora_php_username = "{$os_username}_php";

    /*===
        to ask to use blah_php's password to log in as blah,
        it needs to be in the form BLAH_PHP[BLAH]
    ===*/

    $conn_username = "{$ora_php_username}[{$os_username}]";

    /*===
        how to get the needed password
    ===*/

    $ora_php_password =
        trim(file_get_contents("/home/{$os_username}/.oraauth"));

    /*===
        we are using a connection string of null
        to rely on admin-set-up environmental variables,

        an encoding of 'AL32UTF8' to "[specify] the character 
        set of the database to prevent any need for negotiation", 

        and a session mode whose value is the 
        PHP constant OCI_DEFAULT
     ===*/

    $conn = oci_connect(username: $conn_username,
                        password: $ora_php_password,
                        connection_string: null,
                        encoding: 'AL32UTF8',
                        session_mode: OCI_DEFAULT);

    /* complain and exit if connection failed */
    
    if (! $conn)
    {
        ?>
        <p> Could not log into Oracle, sorry. </p>

    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
	<a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
	or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>
        <?php

        /*===
	    and in this connection-failed case,
            now EXIT this PHP document HERE and NOW,
	    reasonable when you hit an error such that there
	    is NO POINT in going any further
        ===*/

        exit;
    }

    /*===
        HOWEVER -- IF reach here, connection succeeded!
    ===*/
    ?>

    <p> YAY!!!!!!! we connected! </p>

    <?php
    /*=== so we will try a simple SQL query ===*/

    // use oci_parse to set up the statement and return a
    //     statement object

    $date_query = "select sysdate
                   from dual";

    $date_stmt = oci_parse($conn, $date_query);

    // we have no bind variables to bind in this
    //     STATIC SQL statement, so use oci_execute
    //     to now execute the statement

    oci_execute($date_stmt, OCI_DEFAULT);

    // this query can ONLY return ONE row, so...
    //     use oci_fetch to fetch that single row

    oci_fetch($date_stmt);

    // and use oci_result to obtain a desired value from that
    //     fetched row

    $today = oci_result($date_stmt, 1);
    ?>

    <p> Today is: <?= $today ?> </p>

    <?php
    // we are done with this statement, so use oci_free_statement
    //    to free it (free the resources associated with this statement
    //    object)

    oci_free_statement($date_stmt);

    // and, since we are done with this connection now,
    //    TERMINATE it using oci_close!    

    oci_close($conn);
    ?>
    
    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
	<a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
	or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>