With PHP you can easily connect to the database, request the data and display it in your Web site, or even modify the data in the database. MySQL is a very popular database, and there are many tutorials about PHP and MySQL on the internet. MySQL is free, which may attract a lot of people. Because of its wide application, I do not want to repeat the use of MySQL here. Oracle is heavily used in enterprise applications, so we use Oracle to introduce PHP's connection to the database. We certainly don't mention the Oracle database design because it's beyond the scope of our discussion.
PHP provides two sets of functions that are connected to Oracle, namely the Ora_ and OCI functions. Where the Ora_ function is slightly stale. The OCI function update is said to be better. The use of the syntax is almost the same. As mentioned earlier, your PHP installation options should be able to support both use.
For more information on installing the PHP3 Apache server on a Microsoft Windows platform and more about Oracle databases, consult the following url:www.csoft.net/~vsbabu/articles/oraphp.html.
1 connections
?
if ($conn =ora_logon ("User@tnsname", "password"))
{
echo "<b>success! Connected to database<b>\n ";
}
Else
{
echo "<b>failed:-( Could not connect to database<b>\n ";
}
Ora_logoff ($conn);
Phpinfo ();
?>
The above code connects to the database using the Oracle database name, user name, and password defined by Tnsname (indicated in your Tnsnames.ora file). On the basis of a successful connection, the Ora_logon function returns a Non-zero connection ID and stores it in the variable $conn.
2 Query
Assuming the database is already connected, let's actually apply the query to the database. The following code shows a typical example of a connection and query:
?
/*
* Connect to the database and execute the query
*/
function Printoraerr ($in _cur)
{
Check Oracle for errors
Display if there is an error
Call this function every time Oracle is requested when the pointer is activated
if (Ora_errorcode ($in _cur))
echo "Oracle code-". Ora_error ($in _cur). " \ n ";
Return
}
/** Main program * *
if (!) ( $conn =ora_logon ("User@tnsname", "password"))
{
echo "Connection to Database failed\n";
Exit
}
echo "Connected as connection-<b> $conn </b><br>\n";
echo "Opening cursor ... <br>\n";
$cursor =ora_open ($conn); Printoraerr ($cursor);
echo "opened cursor-<b> $cursor </b><br>\n";
$qry = "Select user,sysdate from Dual";
echo "Parsing the query <b> $qry </b> ... <br>\n";
Ora_parse ($cursor, $qry, 0); Printoraerr ($cursor);
echo "Query parsed <br>\n";
echo "Executing cursor ... <br>\n";
Ora_exec ($cursor); Printoraerr ($cursor);
echo "Executed cursor<br>\n";
echo "Fetching cursor ... <br>\n";
while (Ora_fetch ($cursor))
{
$user =ora_getcolumn ($cursor, 0); Printoraerr ($cursor);
$sysdate =ora_getcolumn ($cursor, 1); Printoraerr ($cursor);
echo "row = <B> $user, $sysdate </b><br>\n";
}
echo "fetched all records<br>\n";
echo "Closing cursor ... <br>\n";
Ora_close ($cursor);
echo "Closed cursor<br>\n";
echo "Logging off from Oracle ... <br>\n";
Ora_logoff ($conn);
echo "Logged off from Oracle <br>\n";
?>
(Translator Note: The above code snippet is missing comments, please refer to the Oracle database function section of PHP manual)
3 Display Results
The following code shows how to query the database and output the results:
?
function Printoraerr ($in _cur, $conn)
{
Check Oracle for errors
Display if there is an error
Call this function every time Oracle is requested when the pointer is activated
If it encountered an error, we exit immediately
if (Ora_errorcode ($in _cur))
{
echo "Oracle code-". Ora_error ($in _cur). " <br>n ";
Ora_logoff ($conn);
Exit
}
Return
}
function Exequery ($w _qry, $conn)
{
$cursor =ora_open ($conn); Printoraerr ($cursor, $conn);
Ora_parse ($cursor, $w _qry,0); Printoraerr ($cursor, $conn);
Ora_exec ($cursor); Printoraerr ($cursor, $conn);
$numrows = 0;
$w _numcols=ora_numcols ($cursor);
Show Head
echo "
<table width=\ "100%\" Border=\ "0\" cellspacing=\ "1\" cellpadding=\ "2\" >
<tr>\n ";
for ($i =0; $i < $w _numcols; $i + +)
{
$align = (Ora_columntype ($cursor, $i) = = "Number")? " Right ': Left ';
echo "\t<th valign=top align= $align >". Ora_columnname ($cursor, $i). " </th>\n ";
}
echo "</tr>\n";
while (Ora_fetch ($cursor))
{
echo "<tr>\n";
for ($i =0; $i < $w _numcols; $i + +)
{
$align = (Ora_columntype ($cursor, $i) = = "Number")? " Right ': Left ';
if (Ora_columntype ($cursor, $i) = = "LONG")
echo "<td valign=top align= $align ><PRE>".
Ora_getcolumn ($cursor, $i). " </pre></td>\n ";
Else
echo "<td valign=top align= $align >". Ora_getcolumn ($cursor, $i). " </td>\n ";
Printoraerr ($cursor, $conn);
}
$numrows + +;
echo "</tr>\n";
}
if ($numrows ==0)
echo "<tr><td colspan=\" $w _numcols\ "><b>query returned no records
</b></td></tr>\n ";
Else
{
echo "<tr>\n";
echo "<th colspan=\" ". ($w _numcols-1). " \ "Align=right>count</th>\n";
echo "<th align=right> $numrows </th>\n";
echo "</tr>\n";
}
echo "</table>\n";
Ora_close ($cursor);
Return
}
Main program
if (!) ( $conn =ora_logon ("User@sid", "password"))
{
echo "Error:cannot connect to database\n";
Exit
}
$qry = "Select
DEPTNO \ "Dept\"
, empno \ "Emp\"
, EMPNM \ "Name\"
, Salary \ "Salary\"
From
Employee
Order by 1,2 ";
Exequery ($qry);
Ora_logoff ($conn);
?>
(Translator Note: The above code snippet is missing comments, please refer to the Oracle database function section of PHP manual)
4 Oracle Login based on HTTP
Confirm the Oracle login by adding the following code before the PHP page code. Note that you must set the $ SID correctly.
?
if (!isset ($PHP _auth_user))
{
Header ("Www-authenticate:basic realm=\" $SID \ "");
Header ("http/1.0 401 Unauthorized");
$title = "Login instructions";
echo "<blockquote>
are not authorized to enter the site
</blockquote> \ n ";
Exit
}
Else
{
if (!) ( $conn =ora_logon ("$PHP _auth_user@ $SID", $PHP _AUTH_PW)))
{
Header ("Www-authenticate:basic realm=\" $SID \ "");
Header ("http/1.0 401 Unauthorized");
$title = "Login instructions";
echo "<blockquote>
are not authorised to enter the site
</blockquote> \ n ";
Exit
}
}
?>