PHP-llibreria OCI8: programació PHP. Procediments
Funcions de la llibreria OCI d'Oracle per a PHP:
http://es2.php.net/manual/en/ref.oci8.php
Contingut
- 1 objectiu
- 2 dades que utilitzem
- 3 fer una select bàsica I: recordset_0.php
- 4 fer una select bàsica II: recordset.php
- 5 proc1.php: execució bàsica d'un procediment
- 6 proc2.php: passar paràmetres d'entrada
- 7 proc3.php: passant paràmetres des d'una variable PHP
- 8 proc4.php: paràmetres d'entrada i de sortida
- 9 proc5.php: cridar a una funció
- 10 inserir_empleat.php: formulari per introduir valors
- 11 Annex
objectiu
Tenim instal.lada la llibreria OCI per connectar-nos a Oracle des de PHP. En aquests exercicis mostrarem les operacions bàsiques per accedir a la base de dades, posant especial ènfasi en l'execució de procediments i passar paràmetres i rebre informació d'aquests procediments. Aquests exercicis estan testejats sobre Ubuntu.
En el servidor, el codi està a /usr/share/oracle-php, i puc accedir al codi des de la direcció
http://192.168.0.10/ec/empleat/oracle/
Això està configurat en el Apache (/etc/apache2/sites-available/default)
dades que utilitzem
CREATE TABLE empleat ( id_empleat NUMBER(3) PRIMARY KEY, nom VARCHAR2(25) NOT NULL, cognom VARCHAR2(25) NOT NULL, funcio VARCHAR2(25), mail VARCHAR2(25), sexe CHAR(1) ); INSERT INTO empleat VALUES (2, 'Maria', 'Cirera', 'Administrador Sistemes', 'mcirera@jbalmes.net', 'D'); INSERT INTO empleat VALUES (3, 'Carme', 'Puig', 'Administrador Sistemes', 'cpuig@jbalmes.net', 'D'); INSERT INTO empleat VALUES (4, 'Joan', 'Pujol', 'Programador Java', 'jpujol@jbalmes.net', 'H'); INSERT INTO empleat VALUES (5, 'Rita', 'Vilarmau', 'Programador VB.NET', 'rvilarmau@jbalmes.net', 'D'); INSERT INTO empleat VALUES (6, 'Anna', 'Puigdollers', 'Programador Web', 'apuigdollers@jbalmes.net', 'D'); INSERT INTO empleat VALUES (7, 'Marta', 'Barceló', 'Programador Web', 'mbarcelo@jbalmes.net', 'D'); INSERT INTO empleat VALUES (9, 'Núria', 'Perelló', 'Dissenyador web', 'nperello@jbalmes.net', 'D'); INSERT INTO empleat VALUES (10, 'Guillem', 'Raspall', 'HelpDesk', 'graspall@jbalmes.net', 'H'); INSERT INTO empleat VALUES (11, 'Esteve', 'Llorens', 'HelpDesk', 'ellorens@jbalmes.net', 'H'); INSERT INTO empleat VALUES (12, 'Raimon', 'Compte', 'Administratiu', 'rcompte@jbalmes.net', 'H'); INSERT INTO empleat VALUES (13, 'Josep', 'Vicenç', 'Administratiu', 'jvicenc@jbalmes.net', 'H'); INSERT INTO empleat VALUES (14, 'Toni', 'Cirici', 'Comercial', 'tcirici@jbalmes.net', 'H'); INSERT INTO empleat VALUES (15, 'Claudi', 'Pellicer', 'Comercial', 'cpellicer@jbalmes.net', 'H'); INSERT INTO empleat VALUES (16, 'Raquel', 'Valldemossa', 'Directiu', 'rvalldemossa@jbalmes.net', 'D'); INSERT INTO empleat VALUES (17, 'Bruna', 'E.', 'Directiu', 'bruna_e@jbalmes.net', 'D'); INSERT INTO empleat VALUES (18, 'Clara', 'D.', 'Directiu', 'clara_d@jbalmes.net', 'D'); INSERT INTO empleat VALUES (19, 'Marieta', 'C.', 'Directiu', 'marieta_c@jbalmes.net', 'D'); INSERT INTO empleat VALUES (20, 'M. Carme', 'B.', 'Directiu', 'carme_b@jbalmes.net', 'D'); INSERT INTO empleat VALUES (21, 'M. Assumpta', 'A.', 'Administrador Sistemes', 'assumpta_a@jbalmes.net', 'D'); INSERT INTO empleat VALUES (22, 'Queralt', 'Z.', 'Administrador Sistemes', 'queralt_z@jbalmes.net', 'D'); INSERT INTO empleat VALUES (23, 'Núria', 'Y.', 'Administrador Sistemes', 'nuria_y@jbalmes.net', 'D'); INSERT INTO empleat VALUES (24, 'Patrícia', 'X.', 'Administrador Sistemes', 'patricia_x@jbalmes.net', 'D'); INSERT INTO empleat VALUES (25, 'Sumpta', 'W.', 'Administrador Sistemes', 'sumpta_w@jbalmes.net', 'D'); INSERT INTO empleat VALUES (26, 'Clara', 'V.', 'Administrador Sistemes', 'clara_v@jbalmes.net', 'D'); INSERT INTO empleat VALUES (27, 'Quim', 'U.', 'Administrador Sistemes', 'quim_u@jbalmes.net', 'H'); INSERT INTO empleat VALUES (28, 'Quimet', 'T.', 'Administrador Sistemes', 'quimet_t@jbalmes.net', 'H'); INSERT INTO empleat VALUES (29, 'Xavi', 'S.', 'Programador Java', 'xavi_s@jbalmes.net', 'H'); INSERT INTO empleat VALUES (30, 'Xavier', 'R.', 'Programador Java', 'xavier_r@jbalmes.net', 'H'); INSERT INTO empleat VALUES (31, 'Roc', 'Q.', 'Programador Java', 'roc_q@jbalmes.net', 'H'); INSERT INTO empleat VALUES (32, 'Pere', 'P.', 'Programador Java', 'pere_p@jbalmes.net', 'H'); INSERT INTO empleat VALUES (33, 'Perot', 'O.', 'Programador Java', 'perot_o@jbalmes.net', 'H'); INSERT INTO empleat VALUES (34, 'Galdrich', 'N.', 'Programador VB.NET', 'galdrich_n@jbalmes.net', 'H'); INSERT INTO empleat VALUES (35, 'Guifré', 'M.', 'Programador VB.NET', 'guifre_m@jbalmes.net', 'H'); INSERT INTO empleat VALUES (36, 'Jofre', 'L.', 'Programador VB.NET', 'jofre_l@jbalmes.net', 'H'); INSERT INTO empleat VALUES (37, 'Tomeu', 'K.', 'Programador VB.NET', 'tomeu_k@jbalmes.net', 'H'); INSERT INTO empleat VALUES (38, 'Tomàs', 'J.', 'Programador VB.NET', 'tomas_j@jbalmes.net', 'H'); INSERT INTO empleat VALUES (39, 'Tània', 'I.', 'Dissenyador web', 'tania_i@jbalmes.net', 'D'); INSERT INTO empleat VALUES (40, 'Guim', 'H.', 'Dissenyador web', 'guim_h@jbalmes.net', 'H'); INSERT INTO empleat VALUES (41, 'Andreu', 'G.', 'Dissenyador web', 'andreu_g@jbalmes.net', 'H'); INSERT INTO empleat VALUES (42, 'Santi', 'F.', 'Dissenyador web', 'santi_f@jbalmes.net', 'H'); INSERT INTO empleat VALUES (43, 'Santiago', 'E.', 'Dissenyador web', 'santiago_e@jbalmes.net', 'H'); INSERT INTO empleat VALUES (44, 'Jaume', 'D.', 'HelpDesk', 'jaume_d@jbalmes.net', 'H'); INSERT INTO empleat VALUES (45, 'Ramon', 'C.', 'HelpDesk', 'ramon_c@jbalmes.net', 'H'); INSERT INTO empleat VALUES (46, 'Llorenç', 'B.', 'Comercial', 'llorenc_b@jbalmes.net', 'H'); INSERT INTO empleat VALUES (47, 'Esteve', 'A.', 'Comercial', 'esteve_a@jbalmes.net', 'H'); INSERT INTO empleat VALUES (1, 'Pere', 'Batista', 'Administrador Sistemes', 'pbatista@jbalmes.net', 'H'); INSERT INTO empleat VALUES (8, 'Jordi', 'Basses', 'Dissenyador web', 'jbasses@jbalmes.net', 'H'); COMMIT;
fer una select bàsica I: recordset_0.php
Per obtenir la informació d'una consulta (SELECT) hi ha diverses funcions que poden ser útils: oci_fetch_all, oci_fetch_array, oci_fetch_row, oci_fetch.
$stmt = oci_parse($ora_conn, $query); oci_execute($stmt); $nrows = oci_fetch_all($stmt, $results);
oci_parse() comprova la sintaxi de la sentència que volem executar, i prepara la sentència per a la seva execució. Amb oci_execute executem la query, pròpiament dit, i amb la funció oci_fetch_all omplim la matriu $results, que és una matriu amb tantes columnes com té la SELECT, i amb tantes files com $nrows.
<?php header("Content-Type: text/html; charset=utf-8"); //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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) // 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<br />"; //echo message if connection does not error echo "Mostrem una llista de tots els empleats<br />"; echo "=======================================<br /><br />"; $query = "select * from empleat"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); oci_execute($stmt) or die ('Can not Execute statment'); echo "<table border=\"1\">\n"; while ($row = oci_fetch_array ($stmt, OCI_BOTH)) { echo "<tr><td>".$row['ID_EMPLEAT']."</td><td>".$row['NOM']."</td></tr>"; } oci_free_statement($stmt); echo "</table>\n"; } oci_close($ora_conn); // close the connection ?>
fer una select bàsica II: recordset.php
Per obtenir la informació d'una consulta (SELECT) hi ha diverses funcions que poden ser útils: oci_fetch_all, oci_fetch_array, oci_fetch_row, oci_fetch.
$stmt = oci_parse($ora_conn, $query); oci_execute($stmt); $nrows = oci_fetch_all($stmt, $results);
oci_parse() comprova la sintaxi de la sentència que volem executar, i prepara la sentència per a la seva execució. Amb oci_execute executem la query, pròpiament dit, i amb la funció oci_fetch_all omplim la matriu $results, que és una matriu amb tantes columnes com té la SELECT, i amb tantes files com $nrows.
<?php header("Content-Type: text/html; charset=utf-8"); //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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) // 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<br />"; //echo message if connection does not error echo "Mostrem una llista de tots els empleats<br />"; echo "=======================================<br /><br />"; $query = "select * from empleat"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); oci_execute($stmt) or die ('Can not Execute statment'); $nrows = oci_fetch_all($stmt, $results); if ($nrows > 0) { echo "<table border=\"1\">\n"; echo "<tr>\n"; foreach ($results as $key => $val) { echo "<th>$key</th>\n"; } echo "</tr>\n"; for ($i = 0; $i < $nrows; $i++) { echo "<tr>\n"; foreach ($results as $data) { echo "<td>$data[$i]</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; } else { echo "No data found<br />\n"; } echo "$nrows Records Selected<br />\n"; oci_free_statement($stmt); } oci_close($ora_conn); // close the connection ?>
proc1.php: execució bàsica d'un procediment
Fixem-nos que aquest procediment és molt tonto... la segona vegada que s'executi donarà un error de clau primària.
create or replace procedure inserir_empleat IS BEGIN insert into empleat(id_empleat,nom,cognom) VALUES(50,'Pere','Poma'); END inserir_empleat; / $query = "begin inserir_empleat; end;";
Fitxer proc1.php:
<?php header("Content-Type: text/html; charset=utf-8"); // create or replace procedure inserir_empleat // IS // BEGIN // insert into empleat(id_empleat,nom,cognom) VALUES(50,'Pere','Poma'); // END inserir_empleat; // / //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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) // 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<br />"; //echo message if connection does not error $query = "begin inserir_empleat; end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); //OCIBindByName($stmt,":var_ftel", &$myFriendTel, 20) or die ('Can not bind variable'); oci_execute($stmt) or die ('Can not Execute statment'); echo "S'ha afegit l'empleat Pere Poma<br />"; } oci_close($ora_conn); // close the connection ?>
proc2.php: passar paràmetres d'entrada
El primer que farem per no tenir problemes amb les claus primàries és crear una seqüència:
CREATE SEQUENCE empleat_seq START WITH 100 INCREMENT BY 1; / select empleat_seq.nextval from dual; select empleat_seq.currval from dual;
Fixem-nos que ara amb la sentència
$query = "begin inserir_empleat2('Maria','Batllori'); end;";
ja estic parametritzant el valor que introdueixo a la base de dades, però estic introduint sempre el mateix valor.
Fitxer proc2.php:
<?php header("Content-Type: text/html; charset=utf-8"); // create or replace procedure inserir_empleat2(nom VARCHAR, cognom VARCHAR) // IS // BEGIN // insert into empleat(id_empleat,nom,cognom) VALUES(empleat_seq.nextval,nom,cognom); // END inserir_empleat2; // / //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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']."<br />"); // print the $ora_conn_erno variable/oci_error() function selecting only the message (human readable) } else { // if it doesn't fail it will proceed with the rest of the script echo "Connection Succesful<br />"; //echo message if connection does not error $query = "begin inserir_empleat2('Maria','Batllori'); end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); oci_execute($stmt) or die ('Can not Execute statment'); } oci_close($ora_conn); // close the connection echo "S'ha afegit l'empleat Maria Batllori<br />"; ?>
proc3.php: passant paràmetres des d'una variable PHP
La novetat rau en el següent codi:
$query = "begin inserir_empleat2(:vnom,:vcognom); end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); $vnom="Jordi"; $vcognom="Martínez"; oci_bind_by_name($stmt, ":vnom", $vnom); oci_bind_by_name($stmt, ":vcognom", $vcognom);
Fitxer proc3.php:
<?php header("Content-Type: text/html; charset=utf-8"); // create or replace procedure inserir_empleat2(nom VARCHAR, cognom VARCHAR) // IS // BEGIN // insert into empleat(id_empleat,nom,cognom) VALUES(empleat_seq.nextval,nom,cognom); // END inserir_empleat2; // / //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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) // 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<br />"; //echo message if connection does not error $query = "begin inserir_empleat2(:vnom,:vcognom); end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); $vnom="Jordi"; $vcognom="Martínez"; oci_bind_by_name($stmt, ":vnom", $vnom); oci_bind_by_name($stmt, ":vcognom", $vcognom); oci_execute($stmt) or die ('Can not Execute statment'); echo "Hem introduït l'empleat Jordi Martínez<br />"; } oci_close($ora_conn); // close the connection ?>
proc4.php: paràmetres d'entrada i de sortida
Fixem-nos com definim ara el procediment inserir_empleat3. Aquest procediment retorna, com si fos una funció, el número d'empleats que tenim. Des de PHP visualitzarem el valor que retorna el procediment.
Fitxer proc4.php:
<?php header("Content-Type: text/html; charset=utf-8"); // create or replace procedure inserir_empleat3(nom VARCHAR,cognom VARCHAR, num_empleats OUT NUMBER) // IS // BEGIN // insert into empleat(id_empleat,nom,cognom) VALUES(empleat_seq.nextval,nom,cognom); // select count(*) into num_empleats from empleat; // END inserir_empleat3; // / // declare // vnum_empleats number(3); // begin // inserir_empleat3('Marta','Capdevila',vnum_empleats); // dbms_output.put_line('Número empleats: ' || vnum_empleats); // end; // / //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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) // 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<br />"; //echo message if connection does not error $query = "begin inserir_empleat3(:vnom,:vcognom,:vnum_empleats); end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); $vnom="Martí"; $vcognom="Tresserres"; $vnum_empleats=0; oci_bind_by_name($stmt, ":vnom", $vnom); oci_bind_by_name($stmt, ":vcognom", $vcognom); oci_bind_by_name($stmt, ":vnum_empleats", $vnum_empleats,32); oci_execute($stmt) or die ('Can not Execute statment'); echo "Número d'empleats: ".$vnum_empleats; } oci_close($ora_conn); // close the connection ?>
proc5.php: cridar a una funció
Les funcions retornen un valor. Una manera de cridar la funció i recuperar el valor que retorna és ficar-la a dins d'una select:
$query = "select num_empleats from dual";
La taula dual és una taula que es pot utilitzar per a aquestes ocasions. Per exemple, la calculadora SQL i l'aplicació Hola món SQL són:
SQL> select 4*6 from dual; SQL> select 'Hola món' from dual;
fitxer proc5.php
<?php header("Content-Type: text/html; charset=utf-8"); // create or replace function num_empleats RETURN NUMBER // IS // vnum_empleats NUMBER(2); // BEGIN // select count(*) into vnum_empleats from empleat; // RETURN vnum_empleats; // END num_empleats; // / //select num_empleats from dual; //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //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); //$ora_conn = oci_connect('joan','bbdd','//oracle/XE'); // 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']."<br />"); // print the $ora_conn_erno variable/oci_error() function selecting only the message (human readable) // 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<br />"; //echo message if connection does not error $query = "select num_empleats from dual"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); oci_execute($stmt); $row = oci_fetch_array ($stmt, OCI_BOTH); echo "Número d'empleats: ".$row[0]."<br />"; echo "o bé<br />"; echo "Número d'empleats: ".$row['NUM_EMPLEATS']; //compte, majúscules } oci_close($ora_conn); // close the connection ?>
inserir_empleat.php: formulari per introduir valors
Finalment, els valors de l'empleat que introduiré a la base de dades vindran normalment d'un formulari:
Fitxer inserir_empleat.php (formulari):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es"> <head> <meta http-equiv="content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="./css_style.css" media="screen" title="LangTrainer" /> <title>Inserir Empleat. PHP + Oracle + Oci8</title> </head> <body> <h2>Formulari d'inserció d'un empleat</h2> <form name="form_empleat" method="post" action="inserir_empleat2.php" > <label style="width: 70px;">Nom:</label> <input id="txt" name="nom" style="width:100px;" value="" /> <br /> <label style="width: 70px;">Cognom:</label> <input id="txt" name="cognom" style="width:100px;" value="" /> <br /> <input type="submit" style="margin-left: 75px;" value="Inserir"/> </form> </body> </html>
Fitxer inserir_empleat2.php (recull les dades del formulari):
<?php header ("Content-Type: text/html; charset=utf-8"); $vnom=trim($_POST['nom']); $vcognom=trim($_POST['cognom']); //oracle connection variables $ora_user = 'joan'; //username $ora_pass = 'bbdd'; //user password $ora_host = 'oracle'; //host name or server ip address $ora_db = 'XE'; //database name $ora_conn = oci_connect($ora_user,$ora_pass,'//'.$ora_host.'/'.$ora_db); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es"> <head> <meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" type="text/css" href="./css_style.css" media="screen" title="LangTrainer" /> <title>Inserir Empleat. PHP + Oracle + Oci8</title> </head> <body> <h2>Resultat de la inserció d'un empleat</h2> <?php if ($vnom<>"" & $vcognom<>"") { if (!ora_conn){ // if variable $ora_conn fails to connect // do the following if it fails $ora_conn_erno = oci_error(); echo ($ora_conn_erno['message']."\n"); } else { $query = "begin inserir_empleat2(:vnom,:vcognom); end;"; $stmt = oci_parse($ora_conn, $query) or die ('Can not parse query'); oci_bind_by_name($stmt, ":vnom", $vnom); oci_bind_by_name($stmt, ":vcognom", $vcognom); oci_execute($stmt) or die ('Can not Execute statment'); echo "Hem introduït l'empleat $vnom $vcognom<br />"; } oci_close($ora_conn); // close the connection } else { echo "Has de proporcionar un nom i un cognom per a l'empleat que vols introduir."; } ?> </body> </html>
Annex
Pàgina web per ajuntar tot el codi, fitxer index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es"> <head> <meta http-equiv="content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="./css_style.css" media="screen" title="LangTrainer" /> <title>Inserir Empleat. PHP + Oracle + Oci8</title> </head> <body> <h2>Tutorial Accés a dades: PHP + Oracle amb Oci8</h2> <ul> <li><a href="recordset_0.php">recordset_0.php</a>: fer una select bàsica I</li> <li><a href="recordset.php">recordset.php</a>: fer una select bàsica II</li> <li><a href="proc1.php">proc1.php</a>: execució bàsica d'un procediment</li> <li><a href="proc2.php">proc2.php</a>: passar paràmetres d'entrada</li> <li><a href="proc3.php">proc3.php</a>: passant paràmetres des d'una variable PHP</li> <li><a href="proc4.php">proc4.php</a>: paràmetres d'entrada i de sortida</li> <li><a href="proc5.php">proc5.php</a>: cridar a una funció</li> <li><a href="inserir_empleat.php">inserir_empleat.php</a>: formulari per introduir valors</li> </ul> </body> </html>
Full d'estils, css_style.css:
body { padding-left: 11em; font-family: Verdana, helvetica,arial, sans-serif; color: purple; background-color: #d8da3d } ul.navbar { list-style-type: none; padding: 0; margin: 0; position: absolute; top: 2em; left: 1em; width: 9em } h1 { font-family: Verdana, Helvetica, Geneva, Arial, SunSans-Regular, sans-serif } ul.navbar li { background: white; margin: 0.5em 0; padding: 0.3em; border-right: 1em solid black } ul.navbar a { text-decoration: none } a:link { color: blue } a:visited { color: purple } address { margin-top: 1em; padding-top: 1em; border-top: thin dotted }
creat per Joan Quintana Compte, febrer 2009