1. Configure the environment:
Oracle8Call-Interface (oci8) is required to access databases above oracle8 ). This expansion module needs oracle8 client function library, so you need to connect to the remote ORACLE database, but also install Oracle client software-can go to the Oracle site for free download-http://www.oracle.com, this is something that many beginners often ignore. Therefore, if you read this article, you should not mention "why I cannot connect to the Oracle Database" on the forum.
(1)First, confirm that the Oracle8i client is installed, and then use net8assistant (provided by the client software) to create a service name.
The service name is the SID of the Oracle database. You can query server_names in the initsid file.
(2)Remove the annotation symbol ";" before; Extension = php_oci8.dll in PHP. ini to enable PHP to load Oracle-supported modules.
. Copy php_oci8.dll to the System32 subdirectory under your windows2000server installation directory. Such as D: \ winnt \ system32, heavy
Start your machine.
(3)Write a test file to check whether the connection is correct (if the service name Sid is test ):
The Scott user is built in Oracle and does not need to be created by yourself. You just need to put the following file in your web root directory. If
If the data is displayed in the database, the connection is normal. If not, you need to check the errors in the previous steps.
Test. php
<?
$ Dbconn = ocilogon ("Scott", "Tiger", "test ");
$ SQL = "select * from EMP ";
$ Stmt = ociparse ($ dbconn, $ SQL );
If (! $ Stmt ){
Echo "Exit;
}
Ociexecute ($ stmt );
While (ocifetchinto ($ stmt, & $ result_array ))
{
Echo
"Empno = $ result_array [0]; ename = $ result_array [1]; job = $ result_array [2]; Mgr = $ result_array [3] <br> ";
}
?>
2. Use PHP to execute the Oracle Stored Procedure
(1)After connecting with sqlplus, create a stored procedure:
Create or replace procedure inoutdemo (
Par_in in varchar2,
Par_in_out in out varchar2,
Par_out out varchar2)
Is
Begin
Par_out: = par_in;
Par_in_out: = par_in | ''| par_in_out;
End;
(2)PHP file:
Sptest. php
<?
//: In is the input variable;: inout is the input and output variable;: out is the output variable. For more information, see the PL/SQL manual of oracle.
$ Conn = ocilogon ("Scott", "Tiger", "test ");
$ Stmt = ociparse ($ Conn, "begin inoutdemo (: In,: inout,: Out); end ;");
Ocibindbyname ($ stmt, ": In", $ in, 32 );
Ocibindbyname ($ stmt, ": inout", $ inout, 32 );
Ocibindbyname ($ stmt, ": Out", $ out, 32 );
$ In = "hello ";
$ Inout = "world! ";
Ociexecute ($ stmt );
Echo "<br> ";
Echo "in =". $ in. "<br> ";
Echo "inout =". $ inout. "<br> ";
Echo "out =". $ out. "<br> ";
?>
Iii. Oracle Database Paging
Although Oracle does not have limit available in MySQL, It is very convenient, but it also has its own processing method. Its Special rownum is very important for paging.
. There are many paging methods, the most common of which is minus.
To display n1-n2 records, you can write:
(1) Select * From tablename where rownum <= n2 minusselect * From tablename where rownum <N1
Note: This statement cannot use order by; otherwise, an error is returned.
(2) the method for moving the pointer down is as follows:
Where: $ page indicates the current page, and $ pagesize indicates the number of records displayed on each page.
For ($ I = 0; $ I <($ page-1) * $ pagesize; $ I ++)
{
@ Ocifetch ($ stmt );
}
The data retrieved with ocifetch ($ stmt) is the record you want to display.
(3) If complex query statements are sorted by order by, use the following method:
Select table_name, table_type from (select rownum rowseq, X. * from (select * from cat order
Table_type) x) Where rowseq between N1 + 1 and N2;
I like the third one most. It is very convenient to use it. Haha.
Other methods will not be introduced. It is very troublesome to use something like Oracle cursors, which is not suitable for PHP.
Iv. Insert special characters
Some characters such as single quotes cannot be processed using addslashes in Oracle, but you can use the CHR function of oracle or add a single quotation mark.
.
For example, SQL> insert into table values ('it' | CHR (39) |'s atest '));
Or SQL> insert into table values ('It's atest '));
Display:
It's a test.
V. php and Oracle Transaction Processing
Ociexecute () function: intociexecute (INT statement [, int mode])
The second parameter has two modes: oci_commit_on_success by default, which can be omitted. Oci_default indicates transaction)
Submitted. It is not automatically submitted.
If you have two statements in the program that operate the database at the same time and need to be successfully executed at the same time, roll back If one fails, you can write as follows:
$ Conn = ocilogon ($ username, $ password, $ Sid );
// First sentence
$ SQL = "insert into tablename values ()";
$ Stmt = ociparse ($ Conn, $ SQL );
$ Result = ociexecute ($ stmt, oci_default );
If (! $ Result ){
Ocirollback ($ conn); // rollback if the call fails
Ocifreestatement ($ stmt); // release resources
Ocilogoff ($ conn );
}
// Second sentence
$ SQL = "Update tablename set ..";
$ Stmt = ociparse ($ Conn, $ SQL );
$ Result = ociexecute ($ stmt, oci_default );
If (! $ Result ){
Ocirollback ($ conn); // rollback if the call fails
Ocifreestatement ($ stmt); // release resources
Ocilogoff ($ conn );
}
Ocicommit ($ conn); // submit if both are successful
Ocifreestatement ($ stmt); // release resources
Ocilogoff ($ conn );
6. Use PHP to manipulate Oracle lob data (including image storage and display processing)
For PHP programmers, the biggest headache for Oracle is to use lob to process images.
(1)PHP operations BLOB:
Create a table to save the image. User-uploaded image files are stored in blob
Create Table pictures (
ID number,
Imgtype, varchar2 (60 ),
Description varchar2 (100 ),
Picture blob
);
To automatically add IDs, create a sequence:
Create sequence pic_seq;
PHP Program-insert part:
<?
$ Conn = ocilogon ($ username, $ password, $ Sid );
// Note the following two points: first, use the empty_blob () function. This is an internal letter from Oracle.
// Number, returns a lob operator. When inserting a lob, you can only use this method to create one.
// Empty lob operator, and then operate on the operator. Empty_blob () function is a needle
// For blob type, which corresponds to clob is empty_clob (). Second,
// Returns picture so that php oci functions can process it.
$ Stmt = ociparse ($ Conn, "insert into pictures (ID, imgtype, description, picture) Values
(Pic_seq.nextval, '$ imgtype', '$ description',' $ lob_upload_type ', empty_blob () Returning picture
Into: Picture ");
// Generate the descriptor of a local lob object. Note the second parameter of the function: oci_d_lob,
// Generate a lob object. Other possibilities include oci_d_file and oci_d_rowid.
// Do not correspond to bfile and rowid objects.
$ Lob = ocinewdescriptor ($ Conn, oci_d_lob );
// Bind the generated lob object to the locator returned by the preceding SQL statement.
Ocibindbyname ($ stmt, ': Picture', & $ lob,-1, oci_ B _blob );
Ociexecute ($ stmt );
// Method 1: store data into the lob object. Because the source data is a file, the lob object's SaveFile () method is used directly. Lob
Other methods of the object include Save () and load (), which are used to save and retrieve data respectively. However, the bfile type has only one method: Save ()
If ($ lob-> SaveFile ($ lob_upload )){
Ocicommit ($ conn );
Echo "uploaded successfully <br> 〉";
} Else {
Echo "Upload Failed <br> 〉";
}
// Method 2: Use the Save method to save
// $ Fp = fopen ($ lob_upload, "R ");
// $ File-> Save (fread ($ FP, filesize ($ lob_upload )));
// Fclose ($ FP );
// Release the lob object
Ocifreedesc ($ LOB );
Ocifreestatement ($ stmt );
Ocilogoff ($ conn );
?>
TIPS: Select dbms_lob.getlength (picture) frompictures can be used in sqlplus to check whether the file has been saved to Data.
Library or use the strlen () function in the PHP program.
PHP Program-display part (getpicture. php ):
<?
$ Conn = ocilogon ($ username, $ password, $ Sid );
$ Stmt = ociparse ($ Conn, "select imgtype, picture from pictures whereid = $ pictureid ");
If (ocifetchinto ($ stmt, $ result ))
{
Header ("Content-Type:". $ result [0]);
Echo $ result [1]-> load ();
}
// Use strlen ($ result [1]-> load () to view the image size to check whether the image is properly stored in the database.
?>
To display an image, you only need:
The image is displayed.
Some online articles are written using the lob value returned instead of the descriptor method. I did not try it. You can try it.
The Code is as follows:
If (ocifetchinto ($ stmt, $ result, oci_assoc + oci_return_lobs ))
{
Echo "Content-Type:". stripslashes ($ result [imgtype]);
Echo stripslashes ($ result [Picture]);
}
(2)PHP operations clob:
Oracle has a data type named varchar2, which is used to indicate an indefinite string. Varchar2 is also recommended by Oracle. However
There is a problem with varchar2: up to 4000 characters are allowed, which is equivalent to 2000 Chinese characters. If the length of a string in your program
If the degree is greater than 2000 Chinese characters, varchar2 cannot meet the requirements. In this case, you can try clob. Maximum clob and blob Length
It is 4 GB.
The following is an example (refer to the PHP English manual ):
<?
// The text to save
$ Clobtext = "different DR2 ";
// DB connection
$ Conn = ocilogon ("user", "PW", "TNS ");
// Here, the original example uses a stored procedure. You can also use the Blob operation method above.
// For example: $ stmt = ociparse ($ Conn, "insert into table (ID, clobtext) values (text. nextval ,,
Empty_clob () Returning clobtext into: clob ");
$ SQL = "begin tempclobtest_package.saveclob (: clob); end ;";
$ Clob = ocinewdescriptor ($ Conn, oci_d_lob );
$ Stmt = ociparse ($ Conn, $ SQL );
Ocibindbyname ($ stmt, ': clob', & $ clob,-1, oci_ B _clob );
If (! Ociexecute ($ stmt, oci_default) {print_r (ocierror ($ stmt ));}
Else {echo "submitted successfully ";}
If ($ clob-> Save ($ clobtext ))
{
Ocicommit ($ conn );
Echo "submitted successfully ";
}
Else
{
Print_r (ocierror ($ stmt ));
}
// Release resources
$ Clob-> free ();
Ocifreestatement ($ stmt );
?>