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

<!--
    demo connecting from PHP on "new" nrs-projects
    to the Oracle student database - NO-login version

    adapted from an example by Peter Johnson
    adapted by: Sharon Tuttle
    last modified: 2024-03-26

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

<head>
    <title> oracle test - no login </title>
    <meta charset="utf-8" />

    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />

    <link href="try-oracle.css" type="text/css" rel="stylesheet" />
</head>

<body>
    <h1> Connecting PHP to Oracle - New No-Login Version </h1>

    <?php
        // get part of the username from where this is installed

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

        // but the Oracle account you can log into is your username plus
        //    _php

        $ora_php_username = "{$os_username}_php";

        // but to ask to use blah_php's password to log in as blah,
        // we need to express it in the form BLAH_PHP[BLAH]

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

        // grab the password from this user's .oraauth

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

        // oci_connect expects a username,
        //    then a password,
        //    then a connection string (can be null in this particular approach,
        //                              so PHP can build it from env variables),
        //    then the desired character encoding (we'll use "AL32UTF8"),
        //    then the desired session mode (we'll use the default, the constant
        //                                   OCI_DEFAULT)

        $conn = oci_connect($conn_username, $ora_php_password, null,
                            "AL32UTF8", OCI_DEFAULT);

        if (! $conn)
        {
            ?>
            <p> Could not log into Oracle, sorry! </p>
            <?php
            require_once("328footer-plus-end.html");

            // exit this PHP now -- this is reasonable
            //     when you have hit an error and there is NO
            //     point in going forward
            
            exit;
        }
    
        // if reach here, I connected!

        // use oci_parse with your connection object and a string
        //     with the desired SQL statement to create a statement object
        // NOTE: ***NO*** semicolon in the SQL string!!!!!

        $query_stmt = oci_parse($conn, "select sysdate
                                        from dual");

        // use oci_execute with your statement object and OCI_DEFAULT
        //    to ask that the statement be executed

        oci_execute($query_stmt, OCI_DEFAULT);

        // when the statement is a select, use oci_fetch with the
        //     statement object to get access to the next row in
        //     select's result
        // (this select's result only has one row, but still NEED to fetch it!)

        oci_fetch($query_stmt);

        // use oci_result with the statement object and either the desired
        //     projected column's (1-based) position or name to get the
        //     value of that column in the current row

        $today = oci_result($query_stmt, 1);

        ?>
        <p> Today's date: <?= $today ?> </p>
        
        <?php

        // done with your statement object? FREE it!

        oci_free_statement($query_stmt);

        // done with your connection object? CLOSE it!

        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>