Sunday, September 23, 2007

php interview questions and answers

What is PHP and what's it got to do with Oracle?

PHP is a recursive acronym for "PHP Hypertext Preprocessor". It is an open source, interpretive, HTML centric, server side scripting language. PHP is especially suited for Web development and can be embedded into HTML pages. PHP is comparable to languages such as JSP (Java Server Pages) and Oracle's PSP (PL/SQL Server Pages).



This FAQ describes how PHP interacts with the Oracle Database. It assumes that the reader has PHP installed and working. To test if PHP is working, create a simple PHP document, say hello.php:





If PHP is working, you will see "Hello World" below:





echo "Hello world";

phpinfo(); // Print PHP version and config info

?>





Execute hello.php from command line (php hello.php) or open it from a web browser (http://localhost/hello.php) to see the output. If it's not working, PHP is not correctly installed and this FAQ will not help you.



Note that current versions of Oracle's HTTP Server (Apache) does not ship with PHP (mod_php) pre-installed and that Oracle does not support mod_php or the language itself; however, it may support configurations that include mod_php. Future releases of Oracle iAS (Internet Application Server) will support PHP and include instructions on how to install and use PHP.









What is the difference between the OCI and ORA extension modules?

PHP offers two extension modules that can be used to connect to Oracle:



* The normal Oracle functions (ORA); and

* the Oracle Call-Interface functions (OCI).



OCI should be used whenever possible since it is optimised and provides more options. For example, ORA doesn't include support for CLOBs, BLOBs, BFILEs, ROWIDs, etc.









How does one configure PHP to use Oracle?

Follow these steps to prepare your PHP installation for connecting to Oracle databases:



* Download PHP from www.php.net, install as per the install.txt file, and test if everything is working.



* Install the Oracle Client (or Server) software on your machine and configure SQL*Net to connect to your database(s). See the SQL*Net FAQ for details.



* Edit your php.ini file and uncomment the following two lines (only if your version shipped with pre-compiled extension modules):



;extension = php_oci8.dll

;extension = php_oracle.dll



... otherwise, compile PHP with the following options:



--with-oracle=/path/to/oracle/home/dir

--with-oci8=/path/to/oracle/home/dir



* Ensure that your "extension_dir" parameter (in php.ini) points to the location where the above extension files reside.



* Write a small program to test connectivity - see the next question.









How does one connect to Oracle?

Using the OCI Extension Module -




if ($c=OCILogon("scott", "tiger", "orcl")) {

echo "Successfully connected to Oracle.\n";

OCILogoff($c);

} else {

$err = OCIError();

echo "Oracle Connect Error " . $err[text];

}

?>



Using the ORA Extension Module -




if ($c=ora_logon("scott@orcl","tiger")) {

echo "Successfully connected to Oracle.\n";

ora_commitoff($c);

ora_logoff($c);

} else {

echo "Oracle Connect Error " . ora_error();

}

?>



NOTE: You might want to set your Oracle environment from within PHP before connecting, look at this example:




PutEnv("ORACLE_SID=ORCL");

PutEnv("ORACLE_HOME=/app/oracle/product/9.2.0");

PutEnv("TNS_ADMIN=/var/opt/oracle");

...

No comments: