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

<!--
    FIRST example connecting to Oracle! (with a little error handling
        and many explanatory comments added)

    by: Sharon Tuttle
    last modified: 2025-04-05

    you can run this using the URL:

    https://nrs-projects.humboldt.edu/~st10/s25cs328/328lect10-2/try-oracle-explained.php
-->

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

    <?php
        /*=== turning on PHP error reporting for THIS file ===*/

      	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
        // We happen to be using OCI, Oracle Call Interface, to connect
        //     PHP to Oracle

        // oci_connect, to get a connection object from Oracle,
        //    NEEDS several parameters

        // (probably seb-server-dependent) - from URL-to-directory
        //     mapping information on the server end, get part of the
        //     needed campus owner's username

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

        // but, for Humboldt'd PHP-to-Oracle setup, the Oracle account
        //     you can log into using OCI is your username followed
        //     by _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"));

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

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

        // ADDED AFTER CLASS -- if oci_connect fails to make a connection,
        //     the value returned will be "falsey" -- so you can use an if
        //     statement to end gracefully in that case

        if (! $conn)
        {
            ?>
	    <p> Could not log into Oracle, sorry! </p>
	    
</body>
</html>
            <?php

            // in this case, want to exit this PHP document NOW --
	    //     this is reasonable when you have hit an error situation
	    //     so bad that there is NO point in going any further

            exit;
        }
    
        // SO -- you will ONLY reach here if OCI succeeded in connecting
	//    to Oracle!

        // it is convenient to put the SQL statement (or PL/SQL function/procedure
	//     call) into its own string variable
	// NOTE: do NOT include a semicolon WITHIN the SQL statement string!!!!

        $date_query_str = "select sysdate
                           from dual";

        // use oci_parse with your connection object and a string
        //     with the desired SQL statement to ask Oracle to create
	//     a statement object -- it returns a statement identifier,
	//     according to https://www.php.net/manual/en/function.oci-parse.php

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

        // use oci_execute with the statement object/identifier and OCI_DEFAULT
	//     to ask Oracle to now actually execute that statement

        oci_execute($date_stmt, OCI_DEFAULT);

        // for a SQL select statement, you must call oci_fetch with the
	//     statement object/identifier to get access to the first/next row
	//     in the select statement's result
	//     (according to:
	//     https://www.php.net/manual/en/function.oci-fetch.php
	//     ...it fetches the next row into internal buffers accessible
	//     using oci_result)

        oci_fetch($date_stmt);

        // use oci_result with the statement object/identifier and either the desired
        //     projected expression's (1-based) position or name to get the
        //     value of that column in the current row
	// FUN FACT: if a column name was projected, must give its name 
	//     as an ALL-UPPERCASE string literal! (likewise for a function call;
	//     see below!)
        ?>

        <p> Today's date is <?= oci_result($date_stmt, 1) ?> </p>

        <?php
	// replace the following with "sysdate" to see that that WON'T work
	?>

        <p> Today's date is <?= oci_result($date_stmt, "SYSDATE") ?> </p>

        <?php

        // done with your statement object? FREE it! (ask that its resources
	//     be released)

	oci_free_statement($date_stmt);

        // request whatever other database actions you'd like

        // 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>