|
Previous <
1
2
3
4
5
6
7
8
> Next |
| Perl and PHP (INTERVIEW QUESTIONS) |
4 |
|
|
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.
|
|
|
|
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();
}
?>
|
|
|
|
When using the OCI extension Module, the OCIError() function can be used to obtain an array with error code, message, offset and SQL text. One can also obtain the error for a specific session or cursor by supplying the appropriate handle as an argument to OCIError(). Without any arguments, OCIError() will return the last encountered error. $err = OCIError();
var_dump($err);
print "\nError code = " . $err[code];
print "\nError message = " . $err[message];
print "\nError position = " . $err[offset];
print "\nSQL Statement = " . $err[sqltext];
?>
When using the ORA Extension Module, one can use the ora_error() and ora_errorcode() functions to report errors:
print "\nError code = " . ora_errorcode();
print "\nError message = " . ora_error();
?>
|
|
|
|
The following example creates a procedure with IN and OUT parameters. The procedure is then executed and the results printed out.
// Connect to database...
$c=OCILogon("scott", "tiger", "orcl");
if ( ! $c ) {
echo "Unable to connect: " . var_dump( OCIError() );
die();
}
// Create database procedure...
$s = OCIParse($c, "create procedure proc1(p1 IN number, p2 OUT number) as " .
"begin" .
" p2 := p1 + 10;" .
"end;");
OCIExecute($s, OCI_DEFAULT);
// Call database procedure...
$in_var = 10;
$s = OCIParse($c, "begin proc1(:bind1, :bind2); end;");
OCIBindByName($s, ":bind1", $in_var);
OCIBindByName($s, ":bind2", $out_var, 32); // 32 is the return length
OCIExecute($s, OCI_DEFAULT);
echo "Procedure returned value: " . $out_var;
// Logoff from Oracle...
OCILogoff($c);
?>
|
|
|
|
Unfortunately PHP does not offer connection pooling. One can open "persistent" Oracle connections with the ora_plogon() and OCIPLogon() function calls. Nevertheless, persistent connections do not scale as well as connection pooling. A persistent connection will be kept open for a process, but it will not allow connections to be shared between different processes.
Third party tools like SQL Relay can be used to enable connection pooling for Oracle and other databases.
|
|
|
|
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
|
|
|
|
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)");
CIBindByName($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);
?>
|
|
|
|
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. 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.
|
|
|
|
put sleep function in php script or with the help of set_time_limit
|
|
|
|
Previous <
1
2
3
4
5
6
7
8
> Next |
|
|