Sunday, September 23, 2007

php interview questions and answers

Why do we get error "Call to undefined function: ora_logon()/ ocilogon()"?

PHP is not using the correct extension module. Try compiling PHP with the following options:



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

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



On Windows systems one can just uncomment the following lines in the php.ini file:



;extension = php_oci8.dll

;extension = php_oracle.dll





How does one SELECT, INSERT, UPDATE and DELETE data from PHP?

The following example demonstrates how data can be SELECTed and manipulated via INSERT, UPDATE and DELETE statements:




$c=OCILogon("scott", "tiger", "orcl");

if ( ! $c ) {

echo "Unable to connect: " . var_dump( OCIError() );

die();

}



// Drop old table...

$s = OCIParse($c, "drop table tab1");

OCIExecute($s, OCI_DEFAULT);



// Create new table...

$s = OCIParse($c, "create table tab1 (col1 number, col2 varchar2(30))");

OCIExecute($s, OCI_DEFAULT);



// Insert data into table...

$s = OCIParse($c, "insert into tab1 values (1, 'Frank')");

OCIExecute($s, OCI_DEFAULT);



// Insert data using bind variables...

$var1 = 2;

$var2 = "Scott";

$s = OCIParse($c, "insert into tab1 values (:bind1, :bind2)");

OCIBindByName($s, ":bind1", $var1);

OCIBindByName($s, ":bind2", $var2);

OCIExecute($s, OCI_DEFAULT);



// Select Data...

$s = OCIParse($c, "select * from tab1");

OCIExecute($s, OCI_DEFAULT);

while (OCIFetch($s)) {

echo "COL1=" . ociresult($s, "COL1") .

", COL2=" . ociresult($s, "COL2") . "\n";

}



// Commit to save changes...

OCICommit($c);



// Logoff from Oracle...

OCILogoff($c);

?>





How are database transactions handled in PHP?

When using the OCI Extension Module, PHP will commit whenever ociexecute() returns successfully. One can control this behaviour by specifying OCI_COMMIT_ON_SUCCESS (the default) or OCI_DEFAULT as the second parameter to the ociexecute() function call. OCI_DEFAULT can be used to prevent statements from being auto-committed. The OCICommit() and OCIRollback() functions can then be used to control the transaction.



Note that when OCI_DEFAULT is used on any statement handle, it is inherited by the other statement handles for the connection. You cannot use a mix of autocommit/explicit commit on the same connection handle. If you want to do that you need to use ociNLogon() to get a separate handle.



The ORA Extension Module supports an autocommit mode. Use the ORA_CommitOn() and ORA_CommitOff() functions to toggle between autocommit mode and normal mode. When in normal mode (ORA_CommitOff), one can use the ORA_Commit() and ORA_Rollback() functions to control transactions.



If one doesn't commit or rollback at the end of a script, PHP will do an implicit commit. This is consistent with the way SQL*Plus works.

No comments: