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.