In php3.0 and later versions, php has nearly all of the current database processing functions, including Oracle. in this article, we use an instance to introduce how to use these functions to operate Oracle databases. PHP provides two types of APIs (application interfaces) to operate Oracle databases. One is the standard Oracle processing function (ORA) and the other is the Oracle8 call interface letter.
In php3.0 and later versions, php has nearly all of the current database processing functions, including Oracle. in this article, we use an instance to introduce how to use these functions to operate Oracle databases.
PHP provides two types of APIs (application interfaces) to operate Oracle databases. One is the standard Oracle processing function (ORA) and the other is the Oracle 8 Call Interface function (OCI8). The latter can only be used in Oracle 7 or 8 versions. Because OCI8 provides many optimization options, the OCI8 interface should be used whenever possible. Here we use these two function sets for demonstration.
First, the premise of this article is that you have installed the Oracle database environment and the PHP development environment. if you do not understand it, it doesn't matter much. There are many good articles on the Internet for reference.
Step 1: create a database for the experiment
You can ask your database administrator or refer to the Oracle User manual for troubleshooting.
Create a data table with ORA
Even if you have created a data table, take a look at the text section. It tells you how to use php SQL technology to operate Oracle
In this example, a data table is created to store personal email addresses.
Related PHP code:
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = Ora_Logon ("username", "password ");
If ($ connection = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."";
Exit;
}
$ Cursor = Ora_Open ($ connection );
If ($ cursor = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."";
Exit;
}
$ Query = "create table email_info ".
"(Fullname varchar (255), email_address varchar (255 ))";
$ Result = Ora_Parse ($ cursor, $ query );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."";
Exit;
}
$ Result = Ora_Exec ($ cursor );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."";
Exit;
}
Ora_Commit ($ connection );
Ora_Close ($ cursor );
Ora_Logoff ($ connection );
?>
To process the Oracle database, we must first establish a connection with Oracle.
The syntax is Ora_Logon (user, password). return a connectID ..
Note: Before that, we must set the value of the environment variable ORACLE_SID.
Now, we can perform interactive operations on Oracle using the join ID. The data table name is email_info. The table consists of two domains, one for storing the full name of the individual, (for example: Xiaoyue) one for storing the e-mail address such as (xiaoyue@163.net)
You also need a cursor Ora_Open. This cursor is often used to enumerate data. We use Ora_Parse or Ora_Exec to query the result set of Oracle. Ora_Parse to verify the correctness of the SQL syntax, while Ora_Exec executes the corresponding SQL statement. If all the operations are normal, we will run Ora_Commit to confirm.
Create A Table Using OCI
Next we will create an email personal information book. This time the OCI8 API command is used
Related PHP code:
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = OCILogon ("username", "password ");
If ($ connection = false ){
Echo OCIError ($ connection )."
";
Exit;
}
$ Query = "create table email_info ".
"(Fullname varchar (255), email_address varchar (255 ))";
$ Cursor = OCIParse ($ connection, $ query );
If ($ cursor = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
$ Result = OCIExecute ($ cursor );
If ($ result = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
OCICommit ($ connection );
OCILogoff ($ connection );
?>
We can see that the two sections of code have almost the same syntax, with only different function names. Secondly, in OCI8, we do not need to specifically run the command to open the cursor, when the OCIParse system is called, a cursor ID is automatically returned.
Use ORA to input data to the data table 'email _ info'
When a user browses this script, a form consisting of name and email input fields is displayed. when the user adds data and clicks submit, the script saves the name and email to The 'email _ info' data table.
Related PHP code:
If ($ submit = "click "){
// The submit button was clicked!
// Get the input for fullname and email then store it in the database.
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = Ora_Logon ("username", "password ");
If ($ connection = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}
$ Cursor = Ora_Open ($ connection );
If ($ cursor = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}
$ Query = "insert into email_info values ('$ fullname',' $ email ')";
$ Result = Ora_Parse ($ cursor, $ query );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}
$ Result = Ora_Exec ($ cursor );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}
Ora_Commit ($ connection );
Ora_Close ($ cursor );
Ora_Logoff ($ connection );
}
Else {
Echo'
<FORM action = insert. php method = post>
Enter name
<INPUT name = fullname> </INPUT>
Enter the Email address
<INPUT name = email> </INPUT>
<INPUT name = submit type = submit value = click> </INPUT>
</FORM>
';
}
?>
By the way, this script must be saved as insert. php, because insert. php is specified as the form processing program on the called page.
Browsing effect:
Use OCI to input data to the data table 'email _ info'
Same as above, but it is written using OCI.
Related PHP code:
If ($ submit = "click "){
// The submit button was clicked!
// Get the input for fullname and email then store it in the database.
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = OCILogon ("username", "password ");
If ($ connection = false ){
Echo OCIError ($ connection )."
";
Exit;
}
$ Query = "insert into email_info values ('$ fullname',' $ email ')";
$ Cursor = OCIParse ($ connection, $ query );
If ($ cursor = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
$ Result = OCIExecute ($ cursor );
If ($ result = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
OCICommit ($ connection );
OCILogoff ($ connection );
}
Else {
Echo'
<FORM action = insert. php method = post>
Enter name
<INPUT name = fullname> </INPUT>
Enter the Email address
<INPUT name = email> </INPUT>
<INPUT name = submit type = submit value = click> </INPUT>
</FORM>
';
}
?>
By the way, this script must be saved as insert. php, because insert. php is specified as the form processing program on the called page.
Browsing effect:
Use ORA to list all data in the data table 'email _ info'
Next, we will read the database content one by one and display the data in the 'Email _ info' data table in the form of an html table.
Related PHP code:
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = Ora_Logon ("username", "password ");
If ($ connection = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}
$ Cursor = Ora_Open ($ connection );
If ($ cursor = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}
$ Query = "select * from email_info ";
$ Result = Ora_Parse ($ cursor, $ query );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}
$ Result = Ora_Exec ($ cursor );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}
Echo"
";Echo"
| Full Name |
Email Address |
";While (Ora_Fetch_Into ($ cursor, & $ values )){$ Name = $ values [0];$ Email = $ values [1];Echo"
| $ Name |
$ Email |
";}Echo"
";
Ora_Close ($ cursor );
Ora_Logoff ($ connection );
?>
The browsing effect of the program running is as follows:
|
| Name |
Email address |
| Chunhua |
Springflower@163.com |
| Qiu Yue |
Autumnmoon@163.com |
| ... |
... |
|
|
Use OCI to list data in all data tables 'email _ info'
Same as above, but it is written using OCI.
Related PHP code:
PutEnv ("ORACLE_SID = ORASID ");
$ Connection = OCILogon ("username", "password ");
If ($ connection = false ){
Echo OCIError ($ connection )."
";
Exit;
}
$ Query = "select * from email_info ";
$ Cursor = OCIParse ($ connection, $ query );
If ($ cursor = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
$ Result = OCIExecute ($ cursor );
If ($ result = false ){
Echo OCIError ($ cursor )."
";
Exit;
}
Echo"
";Echo"
| Full Name |
Email Address |
";While (OCIFetchInto ($ cursor, $ values )){$ Name = $ values [0];$ Email = $ values [1];Echo"
| $ Name |
$ Email |
";}Echo"
";
OCILogoff ($ connection );
?>
The browsing effect of the program running is as follows:
|
| Name |
Email address |
| Chunhua |
Springflower@163.com |
| Qiu Yue |
Autumnmoon@163.com |
| ... |
... |
|
|