timetable

common.php
Common script required by all timetable scripts.
 
connect.php
Connects to database and creates the temporary table in which data will be inserted.
 
insert.php
Inserts data into the temporary table.
 
list.php
Lists all data inserted into the temporary table.
 
close.php
Closes the reserved database connection.
 

common.php

<?php
    session_start();

    $server = 'odbtp.somewhere.com';

    if( ($id = $_SESSION['id']) ) {
        if( !($con = odbtp_rconnect( $server, $id )) ) {
            $_SESSION['id'] = NULL;
            die;
        }
    }
    else {
        $con = NULL;
    }
?>

connect.php

<?php
    require( 'common.php' );
?>
<html>
<head>
<title>Connect</title>
</head>
<body>
<?php
    if( $con ) {
        echo "Already Connected<p>";
    }
    else {
        $con = odbtp_rconnect( $server, 'DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=mydb;' ) or die;

        $sql = "CREATE TABLE #TimeTable ( "
             . "TimeId int, "
             . "SqlTime datetime, "
             . "StrTime varchar(255), "
             . "SqlTimeNextYear datetime, "
             . "SqlTimeServer datetime DEFAULT (getdate()) )";

        $qry = odbtp_query( $sql ) or die;

        echo "Connected<p>";
        $id = odbtp_connect_id();
        $_SESSION['id'] = $id;
    }
    odbtp_close();
?>
<a href="connect.php">Connect</a><p>
<a href="insert.php">Insert Data</a><p>
<a href="list.php">List Data</a><p>
<a href="close.php">Close Connection <?= $id ?></a>
</body>
</html>

insert.php

<?php
    require( 'common.php' );
?>
<html>
<head>
<title>Insert Data</title>
</head>
<body>
<?php
    if( !$con ) {
        echo 'Not Connected<p>';
    }
    else {
        $time = time();
        $strtime = date ("l dS of F Y h:i:s A", $time );
        $objtime = odbtp_ctime2datetime( $time );
        $objtime->year += 1;

        $sql = "INSERT INTO #TimeTable( TimeId, SqlTime, StrTime, SqlTimeNextYear ) "
             . "VALUES( ?, ?, ?, ? )";

        $qry = odbtp_prepare( $sql ) or die;
        odbtp_input( $qry, 1 ) or die;
        odbtp_input( $qry, 2 ) or die;
        odbtp_input( $qry, 3 ) or die;
        odbtp_input( $qry, 4 ) or die;

        odbtp_set( $qry, 1, $time ) or die;
        odbtp_set( $qry, 2, $time ) or die;
        odbtp_set( $qry, 3, $strtime ) or die;
        odbtp_set( $qry, 4, $objtime ) or die;
        odbtp_execute( $qry ) or die;

        echo "Data Inserted: $strtime<p>";

        odbtp_close();
    }
?>
<a href="connect.php">Connect</a><p>
<a href="insert.php">Insert Data</a><p>
<a href="list.php">List Data</a><p>
<a href="close.php">Close Connection <?= $id ?></a>
</body>
</html>
<?php
    require( 'common.php' );
?>

list.php

<html>
<head>
<title>List Data</title>
</head>
<body>
<?php
    if( !$con ) {
        echo 'Not Connected<p>';
    }
    else {
        $sql = "SELECT * FROM #TimeTable";
        $qry = odbtp_query( $sql ) or die;

        odbtp_bind_field( $qry, 'SqlTime', ODB_CHAR ) or die;

        echo "<table cellpadding=2 cellspacing=0 border=1>\n";
        echo "<tr><td>&nbsp;TimeId&nbsp;</td><td>&nbsp;SqlTime&nbsp;</td>"
           . "<td>&nbsp;StrTime&nbsp;</td><td>&nbsp;SqlTimeNextYear&nbsp;</td>"
           . "<td>&nbsp;SqlTimeServer&nbsp;</td></tr>";

        while( ($rec = odbtp_fetch_array( $qry )) ) {
            $SqlTimeNextYear
                = date( "l dS of F Y h:i:s A",
                        odbtp_datetime2ctime( $rec['SqlTimeNextYear'] ) );

            $SqlTimeServer
                = sprintf( "%02d/%02d/%d %02d:%02d:%02d.%d",
                           $rec['SqlTimeServer']->month,
                           $rec['SqlTimeServer']->day,
                           $rec['SqlTimeServer']->year,
                           $rec['SqlTimeServer']->hour,
                           $rec['SqlTimeServer']->minute,
                           $rec['SqlTimeServer']->second,
                           $rec['SqlTimeServer']->fraction );

            echo "<tr>";
            echo "<td><nobr>&nbsp;$rec[TimeId]&nbsp;</nobr></td>";
            echo "<td><nobr>&nbsp;$rec[SqlTime]&nbsp;</nobr></td>";
            echo "<td><nobr>&nbsp;$rec[StrTime]&nbsp;</nobr></td>";
            echo "<td><nobr>&nbsp;$SqlTimeNextYear&nbsp;</nobr></td>";
            echo "<td><nobr>&nbsp;$SqlTimeServer&nbsp;</nobr></td>";
            echo "</tr>";
        }
        echo "</table><p>\n";

        odbtp_close();
    }
?>
<a href="connect.php">Connect</a><p>
<a href="insert.php">Insert Data</a><p>
<a href="list.php">List Data</a><p>
<a href="close.php">Close Connection <?= $id ?></a>
</body>
</html>

close.php

<?php
    require( 'common.php' );
?>
<html>
<head>
<title>Close Connection</title>
</head>
<body>
<?php
    if( !$con ) {
        echo 'Not Connected<p>';
    }
    else {
        odbtp_close( $con, TRUE );
        echo "Connection Closed<p>";
        $id = NULL;
        $_SESSION['id'] = $id;
    }
?>
<a href="connect.php">Connect</a><p>
<a href="insert.php">Insert Data</a><p>
<a href="list.php">List Data</a><p>
<a href="close.php">Close Connection <?= $id ?></a>
</body>
</html>