Hi everybody,
I have made a document that tells you how to connect to an ORACLE database from a WIN200O system configured with Apache as the webserver and PHP as the fuel.
Pls ignore this if there are already messages that does the same..and sorry for not checking the messages before posting.
Accessing ORACLE database from PHP
Well, I think when you are running short of time or very near to a deadline; I think the easiest but a round about way to connect from php to a remotely installed database server (ORACLE) is through the ODBC interface.
The following steps can be followed if you have configured PHP on a WIN 2000.
Step 1. You need the ORACLE client to be installed on your system (I’ve installed Oracle 8i client on mine) and then use the Net8 Easy Config to point your system to the remote Database. While doing this you need to remember the name of the instance you specified while connecting to the remote system.
Step 2: Check if the files SQLNET.ORA and TNSNAMES.ORA under ORACLE_HOME/NETWORK/ADMIN directory has the entries pointing to the server.Where ORACLE_HOME is the directory pointing to your Oracle or Orant directory, whichever it is installed in.
Step 3: Add the Oracle ODBC driver from Control Panel -> Administrative Tools -> ODBC Sources and then add a System Data Source from the System DSN Tab. Make sure the Service name and the instance name are the same.
Step 4: Then write the following piece of code to test that you are able to connect to the server
<?php
//odbctest.php
//connect to ODBC databases ; PHP,Apache 1.3.x,WIN 2000 - Database ORACLE ; Remote
$dsn = "test"; //the dsn under System DSN tab
$dbuname = "god"; //the username to connect
$dbpwd = "god"; //and the password
//the built in odbc function to connect returns 0 (FALSE)
$connection = odbc_connect($dsn,$dbuname,$dbpwd);
if ($connection == false){
echo odbc_error().": ".odbc_errormsg()."<br>";
exit;
}
else echo "connection established succesfully <br>";
// ---- some dummy queries to try ---
//A select test
$query = "SELECT * FROM EP_ADMINISTRATOR";
//An Insert Query test -- WARNING check data before insert
//$query = "INSERT INTO EP_WEB_SITE VALUES(3,'tootsie.com','http://www.tootsie.com','Adult',100,'on',1)";
//An Update Query
//$query = "UPDATE EP_WEB_SITE SET SECTOR_ID = 4 WHERE ID = 3";
//the query executer
$result = odbc_exec($connection,$query); //you can use odbc_execute also
if(!$result) {
echo odbc_error()." <b>:</b> ".odbc_errormsg()."<BR>";
return 0;
}
else {
echo "Query executed successfully..<br>";
//fetch the result set into an array
$rc = odbc_fetch_into($result, $resarray);
if(!$rc) echo "fetch not success <br>";
}
//unpack the array and display the results
if(isset($resarray)){
while(list($k,$v) = each($resarray))
echo "Key: $k ; Value: $v <br>";
}
else
echo "Unpack error";
?>
Step Last: I would appreciate any feedback from all of you as I’m a beginner to PHP.