Home Links
Home Page
Use XML in PHP
Compression of the data on PHP
Use mod_perl
Style of coding on PHP
Perl and XML. Library of the programmer
Access to databases under management SUBD POSTGRES95
Parsing on Perl
XMLHttpRequest (AJAX) - sending and processing of answers of http-searches with help JavaScript.
Subsys_JsHttpRequest: pumping of the data without perezagruzki pages (AJAX)
The brief description of regular expressions: POSIX and PCRE
Optimization of searches in MySQL
Wound of treelike structures in Databases (Nested Sets)
Oracle / PHP FAQ
The specification and functions DOM in PHP
Not kehshirovat`!
Report PPP
Useful advice{councils} on optimization of ASP-applications
XML: time has come
 

Oracle / PHP FAQ

What is PHP and what it is necessary to have for job with Oracle?


PHP - a recursive acronym " PHP Hypertext Preprocessor ". It is the interpretive programming language with an open initial code. He is intended for generation of webs - documents on the server.


This document describes, how PHP cooperates with database Oracle. It is meant, that at you it is already established and adjusted PHP. To check up his  serviceability it is possible the following script:



<html>

<p> If PHP is working, you will see " Hello World " below: <hr>

<? php

   echo " Hello world ";

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

?>

</html>


Try to execute this code from the command line or open test page in your browser. If the above mentioned example does not work, postpone this document and dobejtes` correct job PHP.



In what a difference between modules OCI and ORA?


Distribution kit PHP includes two modules, intended for job with PHP:


* Standard Oracle-functions (ORA)

* The interface for access to functions Oracle (OCI)


If there is an opportunity of a choice, the preference should be given module OCI as he is more optimized and has the big functionality. For example, module ORA does not support CLOB, BLOB, BFILE, ROWID.



How to collect PHP with support Oracle?


For what to receive efficient sheaf PHP and Oracle, it is necessary to execute the following steps:

* To download distribution kit PHP from an official site www.php.net, to establish it  on the server and to be convinced of his  serviceability

* To establish client Oracle on your server and to adjust connection to your database

* If at you the Windows-server, raskommentirujte the next lines in a file php.ini:


; extension = php_oci8.dll

; extension = php_oracle.dll


Also be convinced of that extension_dir specified the directory containing the specified modules.


If at you the Linux-server, peresoberite PHP from one of options:


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

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


* Write a small test script that it will be convinced that installation has come to the end successfully. As a test script it is possible to follow an example from the following question.



How it will be connected to a database?


If you use module OCI, use the following code:



<? php

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

  echo " Successfully connected to Oracle.n ";

  OCILogoff ($c);

} else {

  $err = OCIError ();

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

}

?>


If you use module ORA, try the following:



<? php

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 ();

}

?>


The remark: if at you necessary variable environments are not established, place the next lines in the beginning of each script working with Oracle:



<? php

  PutEnv ("ORACLE_SID=ORCL");

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

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


In case you some times within the limits of one script will try will be connected to a database, using an identical combination the login / password, you receive the same connection. If various connections to base are necessary for you, use function OCINLogon (). If constant connections are necessary for you, use function OCIPLogon ().


Why there is a mistake " Call to undefined function: ora_logon () / ocilogon () "?


Most likely, at you support Oracle is not switched on. Return to a question " How to collect PHP with support Oracle? ". It is necessary for you peresobrat` php, if at you Linux or raskommentirovat` loading of corresponding library, if at you Windows.


How to carry out searches SELECT, INSERT, UPDATE and DELETE by means of PHP?

Resulted below initial code shows how to delete / create new tables, to write down / take from them the data in PHP.



<? php

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

  if (! $c) {

    echo " It is impossible it will be connected to base: ". var_dump (OCIError ());

    die ();

}


  // We delete old taboicu

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

  OCIExecute ($s, OCI_DEFAULT);


  // We create the new table

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

  OCIExecute ($s, OCI_DEFAULT);


  // We bring a line in just created table

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

  OCIExecute ($s, OCI_DEFAULT);


  // We bring the data in the table using a design "bind"

  $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);


  // We make sample of a database

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

  OCIExecute ($s, OCI_DEFAULT);

  while (OCIFetch ($s)) {

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

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

}


  // We carry out commit;

  OCICommit ($c);


  // We are disconnected from a database

  OCILogoff ($c);

?>



How to get access to the mechanism of transactions from PHP?


At use of module OCI each time after successful performance ociexecute () automatically occurs commit, and, thus, transaction at once comes to the end. You can operate this process, specifying additional parameter OCI_COMMIT_ON_SUCCESS or OCI_DEFAULT by a call of function ociexecute (). In case of use OCI_DEFAULT you can operate completely the mechanism of transactions, for this purpose use functions OCICommit () and OCIRollback ().


It is necessary to take into account, that use OCI_DEFAULT in one of calls ociexecute () is automatically inherited for all connection to a database and will be used at the further operations with a database. If you do not want to use the automatic and manual mechanism of management of transactions simultaneously, use OCINLogon ().


In case you use module ORA, management of transactions looks a little differently. Use functions ORA_CommitOn () and ORA_CommitOff () for switching between the manual and automatic mechanism of management of transactions. For end of transaction and recoil use functions ORA_Commit () and ORA_Rollback () accordingly.


In case after end of job of a script it was never carried out neither commit, nor rollback, PHP will finish transaction by commit command.



How it is correct to process arising mistakes?


In case you use module OCI, use function OCIError () for reception of the file containing the detailed information on arising mistakes. If it is a little bit{some} connections, OCIError () allows to receive the information on a mistake in everyone them them individually. If the given function is caused without parameters, she returns a file with the information on last occurred mistake.



<? php

  $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];

?>


In case you use module ORA, use functions ora_error () and errorcode () for reception of the information on last occurred mistake.



<? php

  print " nError code = ". ora_errorcode ();

  print " nError message = ". ora_error ();

?>



How to call from PHP stored{kept} procedure?


The example resulted below shows creation and a call of stored{kept} procedure.



<? php

  // We are connected to a database

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

  if (! $c) {

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

     die ();

}


  // We create stored{kept} procedure

  $s = OCIParse ($c, " create procedure proc1 (p1 IN number, p2 OUT number) as ".

                    "begin".

                    " p2: = p1 + 10; ".

                    " end; ");

  OCIExecute ($s, OCI_DEFAULT);


  // We cause 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;


  // We are disconnected from a database

  OCILogoff ($c);

?>