Please send questions to
st10@humboldt.edu .
<html>
<!-- try_oracle1.php
adapted from examples from Peter Johnson
adapted by: Sharon Tuttle
last modified: 11-14-06
-->
<head>
<title> Connecting PHP to Oracle </title>
</head>
<body>
<h2> Connecting PHP to Oracle </h2>
<?php
// do you need to ask for username and password?
if (! array_key_exists('username', $_POST))
{
print <<<PASSWORD_FORM
<form method="post" action="$_SERVER[PHP_SELF]">
<table>
<tr> <td> Username: </td>
<td> <input type="text" name="username">
</tr>
<tr> <td> Password: </td>
<td> <input type="password" name="password">
</tr>
</table>
<br><br>
<input type="submit" value="Log in">
</form>
PASSWORD_FORM;
}
else // you HAVE the username and password
{
$username = $_POST['username'];
$password = $_POST['password'];
// try to connect to Oracle student database on redwood
$db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)
(HOST = redwood.humboldt.edu)
(PORT = 1521)) (CONNECT_DATA = (SID = STUDENT)))";
// now try to log on to this database
$conn = OCILogon($username, $password, $db);
if ($conn == false)
{
echo "Oops, could not log into Oracle server";
echo "</body></html>";
exit;
}
// setting up an Oracle statement, and then executing it
$query = 'select hiredate, salary, commission '.
'from empl';
$stmt = OCIParse($conn, $query);
OCIExecute($stmt, OCI_DEFAULT);
print<<<TABLESTUFF
<table border="1">
<tr>
<th> Hire Date </th> <th> Salary </th> <th> Commission </th> </tr>
TABLESTUFF;
// loop through the results, building a tabular depiction of
// the query results
while ($succ = OCIFetch($stmt))
{
$curr_hiredate = OCIResult($stmt, "HIREDATE");
$curr_salary = OCIResult($stmt, "SALARY");
$curr_commission = OCIResult($stmt, "COMMISSION");
if ($curr_commission == null)
{
$curr_commission = "no commission";
}
print <<<TABLE_ROW
<tr>
<td> $curr_hiredate
</td>
<td align="right"> $curr_salary
</td>
<td align="right"> $curr_commission
</td>
</tr>
TABLE_ROW;
}
// close statement and connection...
OCIFreeStatement($stmt);
OCILogoff($conn);
print " </table>\n";
}
?>
</body>
</html>