<html>
<head>
<title>Retrieve data from database</title>
<style>
a {text-decoration:none}
table {font-family:arial; font-size:11pt}
</style>
</head>
<body bgcolor=lightyellow>
<h1><center>The Student Table</center></h1>
<h2>Retrieve data from Oracle database</h2>
<?php
$host = 'localhost'; #local host
$data = Array(); #define an array
read_data();
display();
//===============================================================================
function display()
{
global $data;
print "<table bgcolor=#eeeeee border=5 width=800 outset>
<tr bgcolor=tan>
<th>Student Id<th>Last Name<th>First Name<th>SSN<th>Sex</tr> \n";
foreach($data as $row)
{
$id = $row[0];
$last = $row[1];
$first = $row[2];
$ssn = $row[3];
$sex = $row[4];
print "<tr valign=top>";
print "<td>$id</td><td>$last</td><td>$first</td><td>$ssn</td><td>$sex</td> \n";
}
print "</table> \n";
}
//===============================================================================
function read_data()
{
global $host, $data;
$host_port = $host;
$DBname = 'orcl';
$DBuser = 'demo';
$DBpswd = 'demo';
$host_port_db = $host_port .'/'. $DBname; #concatenate host_port/db
$connect = oci_connect($DBuser, $DBpswd, $host_port_db); #connect to db server
if (! $connect)
{
$oracle_error = oci_error(); #an array or errors
$error = 'Could not connect: ' . $oracle_error['message'];
print $error;
}
$query = "SELECT *
FROM student
ORDER BY student_id";
$stmt = oci_parse($connect, $query); #execute the query
if (! $stmt)
{
$oracle_error = oci_error($connect);
$error = 'Could parse statement: ' . $oracle_error['message'];
print $error;
}
$result = oci_execute($stmt, OCI_DEFAULT); #execute oracle query
if (! $result)
{
$oracle_error = oci_error($stmt);
$error = 'Could not execute query: ' . $oracle_error['message'];
print $error;
}
$numCols = oci_num_fields($stmt); #number of columns returned
$i=0;
while ($row = oci_fetch_row($stmt)) #loop thru rows
$data[$i++] = $row; #store row in 2 dim array
oci_free_statement($stmt); #free result buffer
oci_close($connect); #close connection
}
//===============================================================================
?>
<?php include "../include.php"; ?> <!-- hyperlink to see the code -->
</body>
</html>