Snippet Name: PHP and Oracle: Connect to database
Description: To connect to Oracle from PHP scripts you'll need to use the oci8 extension (php_oci8.dll). If you installed PHP through your distribution's package management system this extension will most likely be available to you. To enable it, uncomment the following line in php.ini:
extension = php_oci8.dll
If you compiled PHP from source, you will have to reconfigure with the following option:
--with-oci8=$ORACLE_HOME
OCI (Oracle Call Interface) is the name of the driver we use to interact with Oracle. More information on the various options for php_oci8 is available on PHP's website.
The simple script shown to the right will make use of the driver.
Comment: (none)
Language: PHP, ORACLE
Highlight Mode: PHP
Last Modified: March 06th, 2009
|
<?PHP
$conn = ocilogon( "username", "password" );
$query = "SELECT title, author, ref_number, submit_date
FROM test_table";
$parseresults = ociparse($conn, $query);
ociexecute($parseresults);
$rows = ocifetchstatement($parseresults, $rs);
?>
<html><head><title>Simple PHP->OCI Script</title></head><body>
<center><table border=0 cellspacing='0' width='50%'><tr>
<td><b>Author</b></td><td><b>Title</b></td><td><b>Date</b></td></tr>
<tr></tr>
<?PHP
FOR ($iterate = 0; $iterate < $rows; $iterate++){
ECHO "<tr>";
ECHO "<td>" . $rs['author'][$iterate] . "</td>";
ECHO "<td>" . $rs['title'][$iterate] . "</td>";
ECHO "<td>" . $rs['submit_date'][$iterate] . "</td>";
ECHO "</tr>";
}
ECHO "</table><br /><b>$rows recordswere found.</b></body></html>";
?> |