PHP-llibreria OCI8
Segueixo aquest tutorial, que està molt ben explicat:
http://www.acloudtree.com/?tag=php-php5-ubuntu-oci8-oracle-10g-9-8-710
o bé http://techxplorer.com/2008/09/22/installing-the-oci8-library-for-php-5-on-ubuntu-804-810/ (Installing the oci8 library for PHP 5 on Ubuntu 8.04 / 8.10)
que són bàsicament el mateix
Contingut
Instal.lar les dependències
Treballo com a root
$ sudo -i
Assegurar-se de què les següents dependències dels repositoris d'Ubuntu estan instal.lades.
- 1. build-essential - compilers and other tools necessary tools used to build software
- 2. php5-dev - tools and other files necessary to build php modules
- 3. php-pear - tools and other files necessary to download pear modules
- 4. libaio1 - linux kernel aio access library
Instal.lar el Oracle Instant Client
Download the Basic and SDK zip files from the official Oracle website.
Create the following directory to store the instant client files
/opt/oracle/instantclient/
Em descarrego els fitxers:
- oracle-instantclient-basic-10.2.0.4-1.i386.zip
- oracle-instantclient-devel-10.2.0.4-1.i386.zip
Nota important: Download the appropriate Instant Client packages for your platform. All installations REQUIRE the Basic package.
$ mkdir /opt/oracle
move the .zip files in the “Documents” directory to the /opt/oracle directory
$ mv /home/ubuntu/Documents/*.zip /opt/oracle
Change to the /opt/oracle directory
$ cd /opt/oracle
Unzip the files
$ unzip \*.zip
rename instantclient directory
$ mv instantclient_10_2 instantclient
Change directory to instantclient, list the files
$ cd instantclient
Create symbolic links
$ ln –s libclntsh.so.10.1 libclntsh.so $ ln –s libocci.so.10.1 libocci.so
Create a source directory under /opt/oracle . This is where we will house the oci8 libraries.
$ mkdir /opt/oracle/src
Change the directory to /opt/oracle/src and download the oci8 tar using pecl
$ cd /opt/oracle/src $ pecl download oci8
Untar the oci8 libraries
$ tar xvf oci8-1.3.4.tgz
Change to the newly created oci8-1.2.4 directory and issue the following commands
$ cd oci8-1.3.4 $ phpize
Set the ORACLE_HOME environment variable
$ export ORACLE_HOME=/opt/oracle/instantclient $ echo $ORACLE_HOME /opt/oracle/instantclient
Configure oci8 to install with the necessary parameters
$ ./configure --with-oci8=share,instantclient,/opt/oracle/instantclient
run make to compile
$ make
install oci8
$ make install
insert the extension=oci8.so into the php.ini and cli.ini files
$ echo extension=oci8.so >> /etc/php5/apache2/php.ini $ echo extension=oci8.so >> /etc/php5/cli/php.ini
restart apache
$ /etc/init.d/apache2 restart
create a phpinfo.php file in /var/www
$ joe /var/www/phpinfo.php <?php phpinfo(); ?>
Now go to your browser and run the phpinfo file and look for the oci8 module
http://localhost/phpinfo.php
OCI8 Support enabled Version 1.3.4 Revision $Revision: 1.269.2.16.2.38.2.20 $ Active Persistent Connections 0 Active Connections 0 Oracle Instant Client Version 10.2 Temporary Lob support enabled Collections support enabled Directive Local Value Master Value oci8.connection_class no value no value oci8.default_prefetch 100 100 oci8.events Off Off oci8.max_persistent -1 -1 oci8.old_oci_close_semantics Off Off oci8.persistent_timeout -1 -1 oci8.ping_interval 60 60 oci8.privileged_connect Off Off oci8.statement_cache_size 20 20
Download my oratest php template file and configure it with the needed settings.
Test the connection
$ php /home/ubuntu/oratest.php
Connection Succesful
<?php
//oracle connection variables
$ora_user = 'YOUR_USERNAME'; //username
$ora_pass = 'YOUR_PASS'; //user password
$ora_host = 'SERVER_IP_OF_ORACLE"'; //host name or server ip address
$ora_db = 'YOUR_DATABASE'; //database name
// place variable into oci_connect function, then place funtion in variable
$ora_conn = oci_connect($ora_user,$ora_pass,'//'.$ora_host.'/'.$ora_db);
// error handling
if (!ora_conn){ // if variable $ora_conn fails to connect
// do the following if it fails
$ora_conn_erno = oci_error(); // insert oci_error() function into variable
echo ($ora_conn_erno['message']."\n"); // print the $ora_conn_erno variable/oci_error() function selecting only the message (human readable)
oci_close($ora_conn); // close the connection just in case php doesn't close it
} else {
// if it doesn't fail it will proceed with the rest of the script
echo "Connection Succesful\n"; //echo message if connection does not error
oci_close($ora_conn); // close the connection
}
?>
Ara ja podem començar a fer coses més interessants:
funcions de la llibreria OCI8: http://es2.php.net/manual/es
Està ben instal.lada la llibreria, però em dóna un warning
Notes:
- si no em funciona el tnsping, és perque no estan ficades les variables d'entorn. El script del SQL*Plus inicialitza les variables d'entorn
Message 3511 not found; No message file for product=network, facility=TNSTNS-03505: Message 3505 not found; No message file for product=network, facility=TNS
- he tingut bastants problemes perquè he seguit un manual en què per fer oci_connect la sintaxi era:
- $c1 = oci_connect("joan", "bbdd", "XE")
El que funciona realment és
- $c1 = oci_connect("joan", "bbdd", "//oracle/XE")
on oracle és el nom de la màquina o la IP. L'error que tenia era:
Warning: oci_connect() [function.oci-connect]: ORA-12154: TNS:could not resolve the connect identifier specified in /usr/share/oracle-php/prova1.php on line 8
Instal.lació setembre 2009 sobre Oracle 11g
Seguint aquestes instruccions ha funcionat perfectament. Se m'instal.la la versió OCI 1.3.5.
Compte! perquè en el procés d'instal.lació faig
$ export ORACLE_HOME=/opt/oracle/instantclient
però aquest no és el ORACLE_HOME de la meva instal.lació:
root@ubuntu-bbdd:/opt/oracle/src/oci8-1.3.5# export ORACLE_SID=BBDD root@ubuntu-bbdd:/opt/oracle/src/oci8-1.3.5# . oraenv ORACLE_SID = [BBDD] ? The Oracle base for ORACLE_HOME=/u01/app/oracle/product/11.1.0/bbdd is /u01/app/oracle root@ubuntu-bbdd:/opt/oracle/src/oci8-1.3.5# echo $ORACLE_HOME /u01/app/oracle/product/11.1.0/bbdd
creat per Joan Quintana Compte, febrer 2009