spacer Logo  

  Links  |  Get Free Content  |  Post Articles  |  Contact Us  |


Oracle Database XE - PHP/OCI/Apache 

The starting point for this article, is being able to write PHP - and fetch and manipulate data from an Oracle XE installation, without the need for Zend Core or Achievo ATK framework. I am running on an Windows XP pro. Laptop, Toshiba with 512 Meg Ram.

To get you started you need some prerequisites.

Downloads

Oracle Database XE (10G)Apache HTTPD ServerPHP Hypertext processor.

If you plan to use PHP/OCI on an remote Oracle database, you need to have the Oracle instant client for Windows installed as well; It can be downloaded here, from OTN (Oracle Technology Network). The .ZIP file is about 30 MEg and you should choose the Instant Client Basic upon download. 

I am running my Oracle XE locally - if you still don't have an Oracle XE on-board - read about it here. Oracle Database XE - Windows Installation.

Installing Apache 2  

The easiest way is to get the .msi install file apache_2.0.55-win32-x86-no_ssl.msi - after download it starts an installation wizard, that will guide you through the installation. Basic questions like which port should the apace server listen to - default is 80. Where do you want the software to be installed etc. I have put mine in c:\programmer\Apache Group\Apache2\

And that it !

Startup and Shutdown of the apache server

You have two options; Either in the DOS environment or through the Apache Console in your system tray. From DOS you do this;

  1. apache -k stop
  2. apache -k start
  3. apache -k restart

Or as mentioned you can do the same thing through the Apache Console, to control your httpd server.

Depending on where you told the installation wizard to install your software, you will find a sub catalog called CONF. In the CONF catalog you will find the file HTTPD.CONF - for starters pay attention to;

  • DOCUMENTROOT should point to where you want the apache server, to look default for your HTML files. I have set my document_root to c:\u01\wwwroot\ so starting my favorite browser, and entering http://localhost/ or  http://127.0.0.1/ in the address line, tells the server to look in c:\u01\wwwroot for a file (see below).
  • <Directory "C:/u03/wwwroot"> should be set to whatever you set DOCUMENT_ROOT to.
  • DirectoryIndex index.html index.php index.html.var,  Tells the server which file to look for in the directory

To test the installation - entering http://localhost/ should bring up a page saying something like; It worked!

Since this installation is on my local machine - i am not paying to much attention to security - You should if you plan to set this up on an on line server.

Installing PHP

Like the Apache server, download the windows binaries - and choose the one inclusive PHP install wizard. Again the wizard asks a few questions before the installation; Where to install etc. I have installed into c:\u01\php\. Please note your installation destination as an important file will be located here; namely PHP.ini. Its the configuration file for all settings to your current php version.

And thats it!

PHP, Apache and Oracle

Time to put things together.

1. Fire up your favorite editor and open the file PPH.INI. Personally i am using EditPad - you can get it from here. Look for the following line - if its not there add it, if its commented out, uncomment it.

extension=php_oci8.dll

Search for, and make sure that

extension_dir=c:\u01\php\extensions - path_to_where_you_installed_your_php

2. Open the file HTTPD.conf file from your apache server installation CONF catalog, and add

    Options Indexes FollowSymLinks
    AddType application/x-httpd-php .php .phtml
    Action application/x-httpd-php "/php/php.exe"

    ScriptAlias /php/ "c:/u01/php/"

Now Apache knows - that if a file with the extension .php is coming it should be executed by php.exe on my local machine. Restart the apache server either on your DOS command line; Apache -k restart or using the Apache Server console.

Lets try to test it.

In you DOCUMENT_ROOT, edit and save the following file; phpinfo.php

<?php
       phpinfo();
?>

Start you browser and point it to http://localhost/phpinfo.php, locate the text OCI8 and it should look like;

If you have not all ready done so, startup your Oracle XE database. Run services.msc and start the services OracleServiceXE and OracleXETNSListener.

Lets make another file; call it oratest.php, and place that also in your DOCUMENT_ROOT

<?php
$conn  = OCILogon("hr", "hr", "XE");
$query = 'select * from employees';

$stid = OCIParse($conn, $query);
OCIExecute($stid, OCI_DEFAULT);

while ($succ = OCIFetchInto($stid, $row)) {
 foreach ($row as $item) {
        echo $item." ";
     }
     echo "<br>\n";
}

OCILogoff($conn);
?>

Start your browser and enter the following address; http://localhost/oratest.php and you should have a similar output as me below;

 

Lets change the way it looks;

<?php
$conn    = OCIlogon( "hr", "hr","XE");

echo "<html><head><title>PHP / ORACLE XE</title>";
echo "<style>";
echo ".header { background-color: #175B82; font-family: Arial; text-decoration: none; font-size: 21px; color: #FFFFFF; }";
echo "</style>";
echo "</head><body>";
echo "<table border=1 cellspacing='0' width='50%'>";
echo "<tr>";
echo "<td colspan='2'>";
echo "<div class='header'>Data from the HR Account</div><br>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Name</b></td><td><b>Salery</b></td></tr>";

$bgcolor=0;

$query   = "select first_name, salary from employees";
$parsed  = OCIparse($conn, $query);

OCIexecute($parsed);

$nrows = OCIfetchstatement($parsed, $results);

for ($i = 0; $i < $nrows; $i++ )
{
    if ($bgcolor==0) {
 echo "<tr bgcolor='#E4E4E4'>";
 $bgcolor=1;
    } else {
 echo "<tr bgcolor='#CECECE'>";
        $bgcolor=0;
    }
    echo "<td>" . $results["FIRST_NAME"][$i] . "</td>";
    echo "<td>Kr " . number_format($results["SALARY"][$i],   2). "</td>";
    echo "</tr>";
}

echo "<tr><td colspan='2'> Number of records found: $nrows</td></tr></table>";

OCILogoff($conn);
?>

Conclusion

You have installed Apache, PHP and your Oracle XE on your system - you are ready to develop more advanced programs. In the next couple of articles, I will run through basic programming with classes, PHP, Oracle XE, so stay tuned.



View more Free Content by kbirch at GetYourContent.com

Make some cash, Post your articles to our free Article Directory today!






Tags: :
  Get Free Content  |  Post Articles  |  Contact Us  |  Links  |

    If you have any problems, suggestions, or comments please E-mail or call Joe at 1-877-792-3866 ext. 108

5/22/2012 12:04:32 PM