Home > Backend Development > PHP Tutorial > How to use Oracle database in PHP (1)_PHP tutorial

How to use Oracle database in PHP (1)_PHP tutorial

WBOY
Release: 2016-07-21 16:05:33
Original
895 people have browsed it

In php3.0 and above, 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 one: Create an experimental database

You can ask your database administrator or refer to the Oracle user manual to handle this issue. I won’t go into details here

Use ORA to create a data table

Even if you have already created a 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 for storing 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 pass the connection ID has performed interactive operations on Oracle. The name of the data table is email_info. The table consists of 2 fields, one to store a person's full name, (for example: Xiaoyue) and one to store an email address such as (xiaoyue@163.net)

It also requires a cursor Ora_Open. This cursor is often used to enumerate Give 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315656.htmlTechArticleIn php3.0 and above, 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 Ora...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template