Home > Article > Backend Development > How to use Oracle database in PHP_PHP tutorial
php has built-in almost all current database processing functions, including Oracle; in this article we use an example to introduce how to use these functions to operate the Oracle database.
PHP provides 2 major categories of APIs (application programming 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 on Oracle 7 or 8 versions. Because OCI8 provides many optimization options, the OCI8 interface should be used whenever possible. Here we demonstrate using these two function sets respectively.
First of all, the premise of this article assumes that you have already installed the Oracle database environment and PHP development environment. It doesn't matter if you don't understand. There are many related good articles on the Internet for reference.
Step 1: Create a database for experiment
You can ask your database administrator or refer to the Oracle user manual to solve this problem. I won’t go into details here
Create data table using ORA
Even if you have already created the data table, please take a look at this paragraph. It can tell you how to use PHP SQL technology to operate Oracle
In this example we created a data table to store personal emails
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);
?>
In order to deal with the Oracle database, we first need to establish a connection with Oracle.
The syntax is Ora_Logon (user, password), which returns a connectID..
Reminder: Before this we must also set the environment variable: the value of ORACLE_SID.
Now, we can interactively operate Oracle through the connection's ID. The name of the data table is email_info. The table consists of 2 fields, one stores the full name of the individual, (such as: Xiaoyue) and the other stores the email address such as (xiaoyue@163.net)
A cursor Ora_Open is also required. This cursor is often used to enumerate data. We use Ora_Parse or Ora_Exec to query Oracle's result set. Ora_Parse verifies the correctness of the SQL syntax and Ora_Exec executes the corresponding SQL statement. If this all runs normally, then we run Ora_Commit to confirm.
Create A Table Using OCI
Next we will create an email personal information book. This time OCI8 API instructions are used
Related PHP code:
PutEnv("ORACLE_SID=ORASID");
$connection = OCILogon ("username", "password");
if ($connection == false){
echo OCIError($connection)."
";
exit;
}